1

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

4 Answers 4

4

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;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

can u please explain what is the use of keyword "Public, private and protected" for declaring the class variable???? Thx
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.
4

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'] is set, 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;
    }

}

$user_type = isset($_SESSION['wb_user_type']) ? $_SESSION['wb_user_type'] : 'n/a';

$profile = new ModifyProfile($user_type);
?>

Comments

1

do the assignment in constructor.

Comments

1

Try with:

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

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

}
?>

6 Comments

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.
__construct will not work in PHP4 (and because author use "var" instead of public/private/protected that may be important)
Did he say that he use PHP 4 ? Maybe it is his habit ?
He didn't say that, but that's possible.
Because it's bad habit not to declare visibility in PHP5
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.