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

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

 



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
> 

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