Re: Overloading Limitation- Can Someone Confirm?

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

 



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@xxxxxxxxxxx          021 708 334

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