Using __set with arrays solved, but why?

后端 未结 3 1267
萌比男神i
萌比男神i 2021-01-12 16:50

Having done a bit of research, I eventually came across the answer to a question I was soon to ask here anyways; How do you work with arrays via the __get and <

3条回答
  •  甜味超标
    2021-01-12 17:18

    In this particular case, __set is not actually getting called. If you break down what it happening, it should make a bit more sense:

    $tmp = $object->__get('foo');
    $tmp['bar'] = 42
    

    If __get did not return a reference, then instead of assigning 42 to the 'bar' index of the original object, you're be assigning to the 'bar' index of a copy of the original object.

提交回复
热议问题