Using __set with arrays solved, but why?

后端 未结 3 1250
萌比男神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.

    0 讨论(0)
  • 2021-01-12 17:32

    In PHP when you return a value from a function you can consider it making a copy of that value (unless it's a class). In the case of __get unless you return the actual thing you want to edit, all the changes are made to a copy which is then discarded.

    0 讨论(0)
  • 2021-01-12 17:39

    maybe more clear:

    //PHP will try to interpret this:
    $object->foo['bar'] = 42
    
    //The PHP interpreter will try to evaluate first 
    $object->foo
    
    //To do this, it will call 
    $object->__get('foo')
    // and not __get("foo['bar']"). __get() has no idea about ['bar']
    
    //If we have get defined as &__get(), this will return $_data['foo'] element 
    //by reference.
    //This array element has some value, like a string: 
    $_data['foo'] = 'value';
    
    //Then, after __get returns, the PHP interpreter will add ['bar'] to that
    //reference.
    $_data['foo']['bar']
    
    //Array element $_data['foo'] becomes an array with one key, 'bar'. 
    $_data['foo'] = array('bar' => null)
    
    //That key will be assigned the value 42
    $_data['foo']['bar'] = 42
    
    //42 will be stored in $_data array because __get() returned a reference in that
    //array. If __get() would return the array element by value, PHP would have to 
    //create a temporary variable for that element (like $tmp). Then we would make 
    //that variable an array with $tmp['bar'] and assign 42 to that key. As soon 
    //as php would continue to the next line of code, that $tmp variable would 
    //not be used any more and it will be garbage collected.
    
    0 讨论(0)
提交回复
热议问题