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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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?!!

The call to $this->_id in the baseForm class doesn't call __get.

The __get method isn't called when the baseForm checkID() method is
called because it is a method within the baseForm class, thus it has
access to the private instance variable $_id and doesn't use the magic
method.

If you dont define the variable in the class definition (but say, set
it in the constructor) the 'Accessing member' text will appear twice.

--
Marc Gear
marcgear@xxxxxxxxx

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux