Re: Overloading Limitation- Can Someone Confirm?

From: Date: Thu, 30 Mar 2006 20:32:59 +0000
Subject: Re: Overloading Limitation- Can Someone Confirm?
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-232943@lists.php.net to get a copy of this message
Jochem Maas wrote:
Jasper Bryant-Greene wrote:
Jochem Maas wrote:
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']"
No, I'm pretty sure (too lazy and tired right now to test...) that if
you guess wrong :-) .. I couldn't resist testing it: php -r ' class T { private $var = array(); function __set($k, $v) { $this->var[$k] = $v; }
function __get($k)     { var_dump($k); }
} $t = new T; $t->arr = array(); $t->arr["a"] = 1; echo "OUTPUT: \n"; var_dump($t->arr); var_dump($t->arr["a"]); var_dump($t); '
That's weird, because I did get around to testing it before I saw your mail, and in my test it works as *I* expect (PHP 5.1.2)... My comments earlier were based on the fact that it would not be good to limit what can be put in an object through __get and __set effectively to scalar variables, and I would expect the engine developers to realise that. I think they have (unless I've done something stupid in my test code...): Code: <?php class T {
    private $array = array();
    public function __get( $key ) {
        return $this->array[$key];
    }
    public function __set( $key, $value ) {
        $this->array[$key] = $value;
    }
} $t = new T; $t->insideArray = array(); $t->insideArray['test'] = 'testing!'; var_dump( $t ); ?> Output: object(T)#1 (1) { ["array:private"]=> array(1) {
    ["insideArray"]=>
    array(1) {
      ["test"]=>
      string(8) "testing!"
    }
} } -- Jasper Bryant-Greene General Manager Album Limited http://www.album.co.nz/ 0800 4 ALBUM jasper@album.co.nz 021 708 334

« previous php.general (#232943) next »