__get() accessor question (a bug maybe?)

liunx

Guest
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";
}
}
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.

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 (and, actually checkID() is a method of the base class).

Or am I wrong there?

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

Thank you in advance,
TemuriIn 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 (and, actually checkID() is a method of the base class).

In your example, _id is only available within BaseForm. To make it visibile in inputText, you need to give it protected access level, not private.any reason you are using underscores to proceed your variable names? thats a new one for me and in fact when i saw this I immediately thought, "Wait, isn't that against conventional rules"In Python, prepending a "_" to a variable makes it private.
 
Top