note 92958 added to language.oop5.basic

From: Date: Sun, 16 Aug 2009 14:59:57 +0000
Subject: note 92958 added to language.oop5.basic
Groups: php.notes 
Request: Send a blank email to php-notes+get-159544@lists.php.net to get a copy of this message
I hope that this will help to understand how to work with static variables inside a class <?php class a { public static $foo = 'I am foo'; public $bar = 'I am bar'; public static function getFoo() { echo self::$foo; } public static function setFoo() { self::$foo = 'I am a new foo'; } public function getBar() { echo $this->bar; } } $ob = new a(); a::getFoo(); // output: I am foo $ob->getFoo(); // output: I am foo //a::getBar(); // fatal error: using $this not in object context $ob->getBar(); // output: I am bar // If you keep $bar non static this will work // but if bar was static, then var_dump($this->bar) will output null // unset($ob); a::setFoo(); // The same effect as if you called $ob->setFoo(); because $foo is static $ob = new a(); // This will have no effects on $foo $ob->getFoo(); // output: I am a new foo ?> Regards Motaz Abuthiab ---- Server IP: 80.241.173.5 Probable Submitter: 62.196.28.171 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php Edit -- https://master.php.net/note/edit/92958 Del: integrated -- https://master.php.net/note/delete/92958/integrated Del: useless -- https://master.php.net/note/delete/92958/useless Del: bad code -- https://master.php.net/note/delete/92958/bad+code Del: spam -- https://master.php.net/note/delete/92958/spam Del: non-english -- https://master.php.net/note/delete/92958/non-english Del: in docs -- https://master.php.net/note/delete/92958/in+docs Del: other reasons-- https://master.php.net/note/delete/92958 Reject -- https://master.php.net/note/reject/92958 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159544) next »