Re: Overloading Limitation- Can Someone Confirm?

From: Date: Thu, 30 Mar 2006 12:45:59 +0000
Subject: Re: Overloading Limitation- Can Someone Confirm?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-232916@lists.php.net to get a copy of this message
Chris wrote:
While playing with an unnamed framework, I think I discovered an overloading limitation (PHP 5.1.2). Can someone please confirm this limitation? Example class: class testClass {
    public $vars = array();
    public function __get($key)
    {
        return array_key_exists($key, $this->vars) ? $this->vars [$key] : null;
    }
    public function __set($key, $value)
    {
        $this->vars[$key] = $value;
    }
    public function __isset($key)
    {
        return array_key_exists($key, $this->vars);
    }
    public function __unset($key)
    {
        unset($this->vars[$key]);
    }
} Given the above class, the following code will not work: $tc = new testClass(); $tc->arr = array(); $tc->arr['a'] = 'A'; $tc->arr['b'] = 'B'; if (isset($tc->arr['b'])) {
    unset($tc->arr['b']);
} //var_dump is only to see results of above var_dump($tc); Am I a moron or, in fact, does this not work and is a language limitation?
I think its a misunderstanding on the one side and a limitation on the other, you can't use overloading directly on items of an overloaded array e.g: echo $tc->arr['a'] this is triggers a call to __get() with the $key parameter set to something like (I'm guessing) "arr['a']" ... what $key is set to it surely wont be a key in the array testClass->vars (do some test otherwise to see eactly what $key contains if you do "echo $tc->arr['c']" maybe it contains something useful after all) try this: <?php $tc = new testClass(); $tc->a = 'A'; $tc->b = 'B'; if (isset($tc->b)) { unset($tc->b); } var_dump($tc);
Chris chris@dented-planet.net


« previous php.general (#232916) next »