Re: Re: [PHP5]__get, __set and isset()

From: Date: Tue, 31 Aug 2004 14:21:25 +0000
Subject: Re: Re: [PHP5]__get, __set and isset()
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-195284@lists.php.net to get a copy of this message
Bug Id is 29917. Fred. Daniel Schierbeck wrote:
Frédéric hardy wrote:
Is it possible to do something like this : class foo {
    private $array = array();
    function __construct()
    {
        ...
    }
    function __get($key)
    {
        return (isset($this->array[$key]) == false ? null : $this->array[$key]);
    }
    function __set($key, $value)
    {
        $this->array[$key] = $value;
    }
} $foo = new foo(); if (isset($foo->bar) == false)
    $foo->bar = 'bar';
else
    echo $foo->bar;
It seems that isset($foo->bar) return ALWAYS false. Bug ? Fred.
It seems that you are right. I've tried off this code:
    <?php
    class OO
    {
        private $elem = array("a" => 1);
            public function __get ($prop)
        {
            if (isset($this->elem[$prop])) {
                return $this->elem[$prop];
            } else {
                return NULL;
            }
        }
            public function __set ($prop, $val)
        {
            $this->elem[$prop] = $val;
        }
    }
    $o = new OO();
    echo isset($o->a) ? "yes\n" : "no\n";
    echo isset($o->b) ? "yes\n" : "no\n";
    echo is_null($o->a) ? "yes\n" : "no\n";
    echo is_null($o->b) ? "yes\n" : "no\n";
    ?>
I expected something like this:
    yes
    no
    no
    yes
But got this:
    no
    no
    no
    yes
It looks like isset() can't handle object properties correctly. I'll post the bug on php.net.
-- =================================================================== Frederic HARDY Email: fch@hexanet.fr HEXANET SARL URL: http://www.hexanet.fr/ ZAC Les Charmilles Tel: +33 (0)3 26 79 30 05 3, allée Thierry Sabine Direct: +33 (0)3 26 61 77 84 BP 202 - 51686 REIMS CEDEX 2 FRANCE ===================================================================

« previous php.general (#195284) next »