rfc:normalize-array-auto-increment-on-copy-on-write
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:normalize-array-auto-increment-on-copy-on-write [2019/06/19 21:36] – wesnetmo | rfc:normalize-array-auto-increment-on-copy-on-write [2019/06/28 21:13] (current) – wesnetmo | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Normalize | + | ====== PHP RFC: Normalize |
* Version: 0.1 | * Version: 0.1 | ||
* Date: 2019-06-19 | * Date: 2019-06-19 | ||
* Author: Wes (@WesNetmo) | * Author: Wes (@WesNetmo) | ||
- | * Status: Under Discussion | + | * Status: Under discussion |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | When an '' | + | If two '' |
- | to the copy-on-write behavior, it will result in an array that is identical in any way to the | + | the same '' |
- | original one; in fact, the copy also covers the "next auto-increment | + | |
+ | < | ||
+ | assert($array1 === $array2); // identical/ | ||
+ | $array1[] = $array2[] = 123; | ||
+ | assert($array1 === $array2); // still identical/ | ||
+ | </ | ||
+ | |||
+ | This is currently not guaranteed, and because of '' | ||
+ | possible to always enforce this property -- but it should be in some dangerous cases, | ||
+ | namely when functions from (potential) different authors are interacting. | ||
+ | |||
+ | ----- | ||
+ | |||
+ | When an '' | ||
+ | due to the copy-on-write behavior, it will result in an '' | ||
+ | way to the original one, inclusive of its " | ||
<PHP> | <PHP> | ||
Line 19: | Line 34: | ||
$array2 = $array1; | $array2 = $array1; | ||
assert($array2 === [0]); | assert($array2 === [0]); | ||
- | $array2[] = " | + | $array2[] = " |
print_r($array2); | print_r($array2); | ||
Line 29: | Line 44: | ||
</ | </ | ||
- | This behavior happens, unfortunately, | ||
- | " | ||
- | but that in reality are not, because they were misused during their lifetime (for example, | ||
- | it was used '' | ||
- | Despite | + | This happens also between different function scopes. Our functions can receive " |
- | following assertion | + | '' |
+ | reality are not, because they were misused during their lifetime (classic example, it was | ||
+ | used '' | ||
+ | |||
+ | As result of that, despite | ||
+ | scope, the following assertion | ||
<PHP> | <PHP> | ||
Line 50: | Line 66: | ||
test($poison); | test($poison); | ||
</ | </ | ||
- | |||
- | In other words, if '' | ||
- | equal/ | ||
- | them. | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC proposes to reset the "next auto-increment" | + | This RFC proposes to reset the " |
- | on write", | + | write", |
- | The "next auto-increment" | + | " |
- | "next auto-increment" | + | " |
as follows: | as follows: | ||
Line 70: | Line 82: | ||
</ | </ | ||
- | The reset is not limited to new scopes but any new by-value reference: | + | The reset is not limited to new function |
<PHP> | <PHP> | ||
- | | + | $array = [0, 1, 2, 3]; |
- | unset($array[3], | + | unset($array[3], |
- | $arrayCopy = $array; | + | $arrayCopy = $array; |
- | $arrayCopy[] = 2; | + | $arrayCopy[] = 2; |
- | assert($arrayCopy === [0, 1, 2]); // this assertion must pass; it doesn' | + | assert($arrayCopy === [0, 1, 2]); // this assertion must pass; it doesn' |
</ | </ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | This change is not backward compatible; code relying on the next element index being | + | This change is not backward compatible; code relying on the " |
- | remembered between copies of copy-on-write will break. However, the proposed change | + | remembered between copies of copy-on-write will break. However, the proposed change should |
- | should be considered a bug-fix, rather than a behavior change. In fact, it offers | + | be considered a bug-fix, rather than a behavior change; it offers protection against |
- | protection against '' | + | '' |
- | '' | + | and thus will only affect code that is already a candidate for improvements. Furthermore, |
+ | the " | ||
+ | |||
+ | < | ||
+ | $a = [0, 1]; | ||
+ | unset($a[1]); | ||
+ | $b = $a; | ||
+ | $b[] = 2; | ||
+ | // $b is [0 => 0, 2 => 2] | ||
+ | |||
+ | $a = [0, 1]; | ||
+ | unset($a[0], | ||
+ | $b = $a; | ||
+ | $b[] = 2; | ||
+ | // $b is [0 => 2], rather than [2 => 2] | ||
+ | </ | ||
+ | |||
+ | The proposed change would make the behavior consistent and safer. | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
- | Next PHP minor version | + | 7.4 |
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
Line 96: | Line 125: | ||
Vote will require 2/3 majority | Vote will require 2/3 majority | ||
+ | ===== References | ||
+ | |||
+ | * [[https:// |
rfc/normalize-array-auto-increment-on-copy-on-write.1560980201.txt.gz · Last modified: 2019/06/19 21:36 by wesnetmo