views:

97

answers:

1

I am a C# guy trying to translate some of my OOP understanding over to php. I'm trying to make my first class object, and are hitting a few hitches.

Here is the beginning of the class:

<?php

require("Database/UserDB.php");

class User {

  private var $uid;
  private var $username;
  private var $password;
  private var $realname;
  private var $email;
  private var $address;
  private var $phone;
  private var $projectArray;

  public function _construct($username) {

    $userArray = UserDB::GetUserArray($username);
    $uid       = $userArray['uid'];
    $username  = $userArray['username'];
    $realname  = $userArray['realname'];
    $email     = $userArray['email'];
    $phone     = $userArray['phone'];
    $i = 1;
    $projectArray = UserDB::GetUserProjects($this->GetID());
    while($projectArray[$i] != null) {
      $projectArray[$i] = new Project($projectArray[$i]);
    }

UserDB.php is where I have all my static functions interacting with the Database for this User Class. I am getting errors using when I use var, and I'm getting confused. I know I don't HAVE to use var, or declare the variables at all, but I feel it is a better practice to do so.

the error is "unexpected T_VAR, expecting T_VARIABLE"

When I simply remove var from the declarations it works. Why is this?

+7  A: 

You are mixing things up. Before PHP 5 it was

var $uid;

Since PHP 5 it is

private $uid; // or
protected $uid; // or
public $uid;

You can read about it in the Properties documentation:

Note: In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword var in property declarations instead of (or in addition to) public, protected, or private. However, var is no longer required. In versions of PHP from 5.0 to 5.1.3, the use of var was considered deprecated and would issue an E_STRICT warning, but since PHP 5.1.3 it is no longer deprecated and does not issue the warning. If you declare a property using var instead of one of public, protected, or private, then PHP 5 will treat the property as if it had been declared as public.

Felix Kling