rfc:explicit_send_by_ref
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:explicit_send_by_ref [2020/02/20 10:25] – nikic | rfc:explicit_send_by_ref [2022/01/25 18:21] (current) – Move to inactive ilutov | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Explicit | + | ====== PHP RFC: Allow explicit |
* Date: 2017-12-02 | * Date: 2017-12-02 | ||
* Author: Nikita Popov < | * Author: Nikita Popov < | ||
* Proposed for: PHP 8.0 | * Proposed for: PHP 8.0 | ||
* Implementation: | * Implementation: | ||
- | * Status: | + | * Status: |
===== Introduction ===== | ===== Introduction ===== | ||
Line 166: | Line 166: | ||
If the argument is a prefer-ref argument of an internal function, then adding the ''&'' | If the argument is a prefer-ref argument of an internal function, then adding the ''&'' | ||
+ | |||
+ | Just like '' | ||
+ | |||
+ | ==== Forwarding references in __call, call_user_func and similar ==== | ||
+ | |||
+ | The '' | ||
+ | |||
+ | <code php> | ||
+ | class Incrementor { | ||
+ | public function inc(& | ||
+ | $i++; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | class ForwardCalls { | ||
+ | private $object; | ||
+ | public function __construct($object) { | ||
+ | $this-> | ||
+ | } | ||
+ | public function __call(string $method, array $args) { | ||
+ | return $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $forward = new ForwardCalls(new Incrementor); | ||
+ | $i = 0; | ||
+ | $forward-> | ||
+ | var_dump($i); | ||
+ | </ | ||
+ | |||
+ | The above code silently " | ||
+ | |||
+ | The explicit call-site annotation can be used to allow passing the variable by reference: | ||
+ | |||
+ | <code php> | ||
+ | $forward = new ForwardCalls(new Incrementor); | ||
+ | $i = 0; | ||
+ | $forward-> | ||
+ | var_dump($i); | ||
+ | </ | ||
+ | |||
+ | This will make the corresponding element in '' | ||
+ | |||
+ | Similarly, '' | ||
+ | |||
+ | <code php> | ||
+ | function inc(& | ||
+ | |||
+ | $i = 0; | ||
+ | call_user_func(' | ||
+ | // Warning: inc() expects argument #1 ($i) to be passed by reference, value given | ||
+ | var_dump($i); | ||
+ | |||
+ | $i = 0; | ||
+ | call_user_func(' | ||
+ | var_dump($i); | ||
+ | </ | ||
+ | |||
+ | Both of these features are achieved by introducing a new internal argument passing mode '' | ||
==== Differences to the removed " | ==== Differences to the removed " | ||
Line 205: | Line 264: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | |||
- | ==== Requiring call-site annotations ==== | ||
- | |||
- | This RFC only adds support for // | ||
- | |||
- | However, it may be possible to disallow the use of implicit by-reference passing using a declare directive. Directory- or [[rfc: | ||
==== inout / out parameters ==== | ==== inout / out parameters ==== | ||
- | Currently PHP abuses | + | Currently PHP uses by-reference passing as a way to implement '' |
For example the auto-initialization behavior of references is only desired in the case of '' | For example the auto-initialization behavior of references is only desired in the case of '' |
rfc/explicit_send_by_ref.1582194349.txt.gz · Last modified: 2020/02/20 10:25 by nikic