Jasper Bryant-Greene wrote:
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)...

a couple of things: I tested on 5.0.4  yesterday (should have mentioned that)
- just tried on 5.1.1 and the behaviour there _seems_ to agree
with your premise ... but not completely; the outcome is expected but the
code doesn't do what I believe it should be doing with regard to
guarding against arbitrary crap into an object via __set() - I assume people
use __set() to protect variables, in the same way as 'normal' setters
are often used. I modified you test code a little - notice how __set()
is only called once (the second 'set' is actually a 'get'!):

<?php

class T {

    private $array = array();

    public function __get( $key ) {
        echo "Getting $key\n";
        return $this->array[$key];
    }

    public function __set( $key, $value ) {
        echo "Setting $key\n";
        $this->array[$key] = $value;
    }

}

$t = new T;

$t->insideArray = array();
$t->insideArray["test"] = "testing!";

var_dump( $t );

?>

OUTPUT:

Setting insideArray
Getting insideArray
object(T)#1 (1) {
  ["array:private"]=>
  array(1) {
    ["insideArray"]=>
    array(1) {
      ["test"]=>
      string(8) "testing!"
    }
  }


I personally feel that the fact that $t->array['insideArray']['test'] is
being set (why does __set() seem to return a reference) is unwanted ...
it circumvents any variable checking logic I might have in __set()

how do like them apples? ;-)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to