Re: Documentation for VAR

From: Date: Fri, 22 Apr 2005 18:21:06 +0000
Subject: Re: Documentation for VAR
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-213680@lists.php.net to get a copy of this message
Richard Davey wrote:
Hello Satyam, Friday, April 22, 2005, 3:59:38 PM, you wrote: S> I've been going through the manual and haven't found 'var' S> documented anywhere, as far as I found, it is just used, and that's S> it. It seems it first appears when it talks about Classes, and it S> is just used in the examples as if everyone knew what var is S> supposed to do. It's a "constant initializer" - which is both an accurate and misleading title at the same time :) It will initialise a variable to be constant through-out your entire class (meaning any method can access it) but unlike true PHP constants (those created with define) the value of the variable can be manipulated from just about anywhere.
indeed in php you can do the following instead: <? error_reporting( E_ALL & E_STRICT ); class Test {
    // this first var (commented out) should (I thought) act as if defined
    // 'public'	- it doesn't given a parse error with php -l, but does when run.
    // I haven't run into this problem in 1.5 years of writing php5...
    // I must be having a brain freeze :-/
    //$myVar;
    // publically accessible instances variables
    public $myVar1; // not initialized with a value
    public $myVar2 = "2"; // initialized with a value
    // an instance variable thats only available
    // from within _all_ methods of this object.
    // .. methods could be defined in subclasses or parent-classes
    protected $myVar3 = "3";
    // an instance variable that is only available to the
    // methods defined in _this_ class
    private $myVar4 = "4";
    // a variable with class scope (all instances see the same value),
    // publically accessible
    static $myVar5 = "5";
    static public $myVar6 = "6";
    // a variable with class scope (all instances see the same value),
    // thats only available from within _all_ methods of this object.
    static protected $myVar7 = "7";
    // a variable with class scope (all instances see the same value),
    // thats only available from methods defined in _this_ class
    static private $myVar8 = "8";
    public function get4() { return $this->myVar4; }
    public function get8() { return self::$myVar8; }
} // basic usage examples: var_dump( ($t = new Test), $t->myVar2, $t->get4(), Test::$myVar6, $t->get8() ); ?> I can thoroughly recommend php5's much improved object model, try other variations of calls to the defined 'myVar's, also try doing with sublcasses, see what does/doesn't work, have fun :-)
It is of course PHP4 only and is depreciated in PHP5. Best regards, Richard Davey


« previous php.general (#213680) next »