tags:

views:

322

answers:

4

Hi,

I have a problem regarding assign session variable to class variable while initialize the class variable.

Check my below code

<?php 
class ModifyProfile
{
    var $userType=$_SESSION['wb_user_type'];

    var $tablename=WB_CUSTOMER_TABLE;
    var $primarykey="nCustomerID";

}
?>

When i run the above code by creating this class object. its giving the parse error for php.

But when i have declare the first variable to blank then its working fine. Please check the code which is working.

<?php 
class ModifyProfile
{
    var $userType='';

    var $tablename=WB_CUSTOMER_TABLE;
    var $primarykey="nCustomerID";
}
?>

so can i assign the session variable to class variable as above or not.

EDIT:

What is the use of public, private and protected keyword while declaring class variable? I am running on php5.

Thanks

+1  A: 

do the assignment in constructor.

radex
+1  A: 

Try with:

<?php 
class ModifyProfile
{
    var $tablename=WB_CUSTOMER_TABLE;
    var $primarykey="nCustomerID";

    public function __construct() {
        $this->userType = $_SESSION['wb_user_type'];
    }

}
?>
hsz
Better IMHO since you might later use a different way to get the user Type: public function __construct($userType) {$this->userType = $userType; } and later use: $profile = new ModifyProfile($_SESSION['wb_user_type']); You might also read up on dependency injection.
dbemerlin
__construct will not work in PHP4 (and because author use "var" instead of public/private/protected that may be important)
radex
Did he say that he use `PHP 4` ? Maybe it is his habit ?
hsz
He didn't say that, but that's possible.
radex
Because it's bad habit not to declare visibility in PHP5
radex
@hsz The use of the keyword 'var' indicates PHP4. In php5 the 'var' keyword is no longer used, and in PHP4 there is no public type (everything is public)
AntonioCS
+3  A: 

When you assign class variables like that, they actually can't be variable. That is, they have to be a literal value, such as a string, or a constant like in your second example which worked.

Ok:

public $foo = 123;
public $bar = "hello";
public $blah = SOME_CONSTANT;

Not Ok:

public $foo = 123 + 45;
public $bar = "hello"
            . "world";
public $blah = some_function();

What you probably want is instance variables. These are initialised in the class constructor, which is a function which is run whenever you create a new instance of that class.

class Foo {
    public $bar,
           $baths;

    public function __construct($blah) {
        $this->bar = $_SESSION['bar'];
        $this->baths = $blah;
    }
}
nickf
can u please explain what is the use of keyword "Public, private and protected" for declaring the class variable???? Thx
Avinash
It determines where you can access those members from. `public` means it is accessible from anywhere. `private` means it is *only* accessible from inside that very class. `protected` means it is only accessible from that class and all its subclasses. If you use `var` or don't explicitly use `public/private/protected` on your functions, PHP defaults to `public`.
nickf
+2  A: 

Some tipps:

  • get rid of var, use private/protected/public instead.
  • Assign the needed value in the constructor, see @hsz's answer.
  • Don't assume $_SESSION['wb_user_type'] hold's something, it's a bad practice, your class won't be portable.

Good example:

<?php 
class ModifyProfile
{
    protected $tablename=WB_CUSTOMER_TABLE;

    public function __construct($user_type) {
        $this->userType = $user_type;
    }

}

$profile = new ModifyProfile($_SESSION['wb_user_type']);
?>
erenon