__get() accessor question (a bug maybe?)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
temuri
Forum Newbie
Posts: 1
Joined: Wed Sep 20, 2006 3:50 pm

__get() accessor question (a bug maybe?)

Post 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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

this->checkID() invokes the implementation in baseForm, and it has access to its class' private member.
Post Reply