> I think there is a lot to say why is not working but just look at those > 2 execution: > > ---------------- 1st > class A > { > > public function __get($name) > { > $this->$name = array(); > return $this->$name; > } > > public function test() > { > $this->_zork; > $this->_zork['bar'] = 67; > } > } > > $a = new A; > $a->test(); > > var_dump($a); > ---------------- 2nd > class A > { > > public function __get($name) > { > $this->$name = array(); > return $this->$name; > } > > public function test() > { > $this->_zork['bar'] = 67; > } > } > > $a = new A; > $a->test(); > > var_dump($a); > ---------------- > > > Adding something that don't have side effect make the side effect > work.... (more or less) > You almost have to know how the VM is implemented in other to know what > is going on. > Nothing is obvious. >
On the contrary, it's quite obvious what's going on. In both examples __get() returns an array as PHP would normally do it (i.e. NOT by reference) which means that if you try to modify that you'll end up modifying nothing much. However, in your second example, the point at which you call __get() indirectly comes before the assign to the zork array - hence, the $this->zork['blah'] = 'blah'; no longer indirectly calls __get as object::$zork now exists. In other words, this is down to you confusing passing a variable by reference and passing it by value: PHP normally passes arrays by value, so when __get() returns an array, you're working on a copy of the array you returned. As someone noted earlier, you can easily change the behaviour of __get to return variables by reference, should you want to. However, I personally wouldn't want this to be default behaviour as that would make debugging apps much more annoying - things should be consistent, even if consistency at times confuse people. Regards Peter -- <hype> WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 </hype> -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php