Re: __get() accessor question (a bug maybe?)

From: Date: Wed, 15 Nov 2006 16:41:58 +0000
Subject: Re: __get() accessor question (a bug maybe?)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-244680@lists.php.net to get a copy of this message
TemuriI wrote: > Hi all, here is the code I am having problem with: > > ============================================ > > class baseForm { > private $_id = 10; > > function __get($member) > { > echo "Accessing member \"{$member}\" : <br />\n"; > return $this->$member; > } > function checkID() > { > echo $this->_id."<br />\n"; here you are accessing a private member variable that does exist, hence __get() is not triggered by checkID(). make the $_id var protected and you should see that __get() is not triggered for the subclasses' display() method either. > } > } > class inputText extends baseForm > { > function display() > { > $this->checkID(); > echo $this->_id."<br />\n"; > } > } > $f = new inputText(); > echo $f -> display(); > > > ============================================ > > > The question is: why the string 'Accessing member "_id" :' is only > displayed once? If you look at the code - it actually accesses $_id > member TWICE. But __get() gets gets triggered only once. WHY?!! > > From what I see, the __get() accessor function is triggerred only from > OUTSIDE the class. you see correct! but [I think] fail to see that there is a difference between properties that actually exist and properties that don't.... class A { private $props = array("_id" => 1); function testA() { echo $this->_id,"\n"; } function __get($m) { echo "Accessing member \"{$m}\" : \n"; if (isset($this->props[$m])) echo $this->props[$m]; } } class B extends A { function testB() { $this->testA(); echo $this->_id,"\n"; } } $b = new B; $b->testB(); > > PHP Version 5.1.6, installed on Win32 (unfortunately). > > Thank you in advance, > Temuri >

« previous php.general (#244680) next »