Page 1 of 1

__get() accessor question (a bug maybe?)

Posted: Wed Nov 15, 2006 10:21 am
by temuri
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi all, here is the code I am having problem with:

Code: Select all

class baseForm {
   private $_id = 10;

   function __get($member)
   {
       echo "Accessing member \"{$member}\" : <br />\n";
       return $this->$member;
   }
   function checkID()
   {
       echo $this->_id."<br />\n";
   }
}
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 triggered only once. WHY?!!

From what I see, the __get() accessor function is triggerred only from OUTSIDE the class.

Now, I realise that $_id is declared as private. But in order for __get() or __set() to fire up, member either should not be declared at all, or declared as private. In any case, shouldn't private members move over to extending class? They aren't accessible from outside, but should be readable by all methods of extending class. Or am I wrong there?

I have PHP Version 5.1.6, installed on Win32 (unfortunately).

Thank you in advance,
Temuri


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Nov 15, 2006 10:57 am
by volka
this->checkID() invokes the implementation in baseForm, and it has access to its class' private member.