Hello, On Thu, Mar 18, 2010 at 8:49 AM, mathieu.suen <mathieu.s...@easyflirt.com> wrote: > Etienne Kneuss wrote: >> >> Hello, >> >> On Wed, Mar 17, 2010 at 3:40 PM, mathieu.suen >> <mathieu.s...@easyflirt.com> wrote: >> >>> >>> Ionut G. Stan wrote: >>> >>>> >>>> Hi, >>>> >>>> This is interesting and it appears the following change makes the >>>> snippet >>>> work as expected: >>>> >>>> public function &__get($name); >>>> >>> >>> I think is that the $this->anArray['bar'] = 4; >>> >>> Generate the following bytcode: >>> >>> 0 FETCH_OBJ_W $0 'anArray' >>> 1 ZEND_ASSIGN_DIM $0, 'bar' >>> >>> Will the folloing : >>> >>> echo $this->anArray; >>> $this->anArray['bar']; >>> >>> 0 FETCH_OBJ_R $0 'anArray' >>> 1 ECHO >>> ... >>> >>> >>> >>> IMHO I think that the complexity of the VM is way to hight. >>> That is something I am strongly agree with Gilad Bracha on adding new >>> feature into a language: >>> >> >> What exactly would you like it do? You've two options: >> >> 1) __get, define the property, and then __set? >> 2) __get returns a ref that is modified >> >> The second option is taken by PHP. >> In your case you return a value, not a reference. >> > > 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); > ---------------- >
Nothing strange or unexpected here, in "1st" you define $this->_zork in __get the first time you call it, meaning that you'll bypass the __get for your next $this->_zork['foo'] = 2; > > 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. > >> >>> >>> Look at the last paragraph: >>> http://gbracha.blogspot.com/2009/09/systemic-overload.html >>> >>> >>>> >>>> On 3/17/10 3:55 PM, mathieu.suen wrote: >>>> >>>>> >>>>> Hi, >>>>> >>>>> >>>>> I came across a strange behavior when using the magic method __get and >>>>> some instance variable that should be an array. >>>>> Consider the following example: >>>>> >>>>> >>>>> 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); >>>>> >>>>> >>>>> So could someone explain me what is the semantic of the above >>>>> statements? >>>>> >>>>> Thanks >>>>> >>>>> >>>>> -- Mathieu Suen >>>>> >>> >>> --Mathieu Suen >>> >>> >>> -- >>> PHP Internals - PHP Runtime Development Mailing List >>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >>> > > -- Mathieu Suen > > > > > -- Etienne Kneuss http://www.colder.ch -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php