rfc:foreach_unwrap_ref
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:foreach_unwrap_ref [2021/08/13 12:45] – created nikic | rfc:foreach_unwrap_ref [2021/11/14 17:10] (current) – nikic | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Date: 2021-08-13 | * Date: 2021-08-13 | ||
* Author: Nikita Popov < | * Author: Nikita Popov < | ||
- | * Status: | + | * Status: |
* Target Version: PHP 8.2 | * Target Version: PHP 8.2 | ||
* Implementation: | * Implementation: | ||
Line 56: | Line 56: | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC proposes to change the semantics of foreach by reference to unwrap the reference after the loop. This means that '' | + | This RFC proposes to change the semantics of foreach by reference to unwrap the reference after the loop. This means that '' |
While PHP does not have a dedicated language construct for reference unwrapping, the operation is logically equivalent to: | While PHP does not have a dedicated language construct for reference unwrapping, the operation is logically equivalent to: | ||
Line 93: | Line 93: | ||
</ | </ | ||
- | Unwrapping | + | This RFC proposes to only perform |
- | There are broadly two ways we can handle this: Either we always perform | + | The reason is that complex variables may have side effects. The most obvious case is ''& |
+ | |||
+ | Use of complex variables as foreach targets is very unusual, so it is rather unlikely that someone will encounter issues with the reused loop variables in this context. Always performing | ||
+ | |||
+ | When foreach is used in conjunction with destructuring, unwrapping | ||
+ | |||
+ | < | ||
+ | foreach ($array as [&$var, & | ||
+ | </ | ||
+ | |||
+ | In this example, '' | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== |
rfc/foreach_unwrap_ref.1628858748.txt.gz · Last modified: 2021/08/13 12:45 by nikic