rfc:explicit_send_by_ref

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
rfc:explicit_send_by_ref [2020/02/20 10:58] nikicrfc:explicit_send_by_ref [2020/02/20 13:58] nikic
Line 167: Line 167:
 If the argument is a prefer-ref argument of an internal function, then adding the ''&'' annotation will pass it by reference, while not adding it will pass it by value. Outside this mode, the passing behavior would instead be determined by the VM kind of the argument operand. If the argument is a prefer-ref argument of an internal function, then adding the ''&'' annotation will pass it by reference, while not adding it will pass it by value. Outside this mode, the passing behavior would instead be determined by the VM kind of the argument operand.
  
-==== Open Question: Forwarding references in __call, call_user_func and similar ====+==== Forwarding references in __call, call_user_func and similar ====
  
 The ''%%__call()%%'' and ''%%__callStatic()%%'' magic methods can be used to forward calls, but this does not work properly with by-reference arguments: The ''%%__call()%%'' and ''%%__callStatic()%%'' magic methods can be used to forward calls, but this does not work properly with by-reference arguments:
Line 196: Line 196:
 The above code silently "works", but the variable ''$i'' will not actually get passed by reference. A by-reference pass does occur at the ''%%$this->object->$method(...$args)%%'' call, but at this point the value stored in ''$args'' and the variable ''$i'' have no relation to each other. The above code silently "works", but the variable ''$i'' will not actually get passed by reference. A by-reference pass does occur at the ''%%$this->object->$method(...$args)%%'' call, but at this point the value stored in ''$args'' and the variable ''$i'' have no relation to each other.
  
-The explicit call-site annotation could be used to allow passing the variable by reference:+The explicit call-site annotation can be used to allow passing the variable by reference:
  
 <code php> <code php>
Line 205: Line 205:
 </code> </code>
  
-This would make the corresponding element in ''$args'' be a reference, which the argument unpack would pass on to the called function. Making this work would require introducing a new internal argument passing mode like ''prefer-val'', which permits both by-value and by-reference passing, but prefers by-value passing by default (unlike the existing ''prefer-ref'').+This will make the corresponding element in ''$args'' be a reference, which the argument unpack then passes on to the called function.
  
-A similar problem also exists with ''call_user_func()'', which is not capable of calling functions that have by-reference parameters properly. The same mechanism could be used to support this.+Similarly, ''call_user_func()'' can now be used to call functions that accept by-reference parameters
 + 
 +<code php> 
 +function inc(&$i) { $i++; } 
 + 
 +$i = 0; 
 +call_user_func('inc', $i); 
 +// Warning: inc() expects argument #1 ($i) to be passed by reference, value given 
 +var_dump($i); // int(0) 
 + 
 +$i = 0; 
 +call_user_func('inc', &$i); 
 +var_dump($i); // int(1) 
 +</code> 
 + 
 +Both of these features are achieved by introducing a new internal argument passing mode ''prefer-val''. Just like ''prefer-ref'' it accepts both by-value and by-reference, but prefers by-value passing unless by-reference passing is forced by a call-site annotation.
  
 ==== Differences to the removed "call-time pass-by-reference" feature ==== ==== Differences to the removed "call-time pass-by-reference" feature ====
rfc/explicit_send_by_ref.txt · Last modified: 2022/01/25 18:21 by ilutov