rfc:normalize-array-auto-increment-on-copy-on-write

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:normalize-array-auto-increment-on-copy-on-write [2019/06/19 22:40] wesnetmorfc:normalize-array-auto-increment-on-copy-on-write [2019/06/28 21:13] (current) wesnetmo
Line 1: Line 1:
-====== PHP RFC: Normalize array'"auto-increment" value on copy on write ======+====== PHP RFC: Normalize arrays' "auto-increment" value on copy on write ======
  
   * 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://wiki.php.net/rfc/normalize-array-auto-increment-on-copy-on-write   * First Published at: http://wiki.php.net/rfc/normalize-array-auto-increment-on-copy-on-write
  
Line 18: Line 18:
 </PHP> </PHP>
  
-This is currently not guaranteed, and due to PHP arrays' nature, it is not possible to +This is currently not guaranteed, and because of ''array''s' all-doing nature, it is not 
-always enforce this property -- but it should be, at least in some dangerous cases.+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 ''array'' is assigned to a new reference, and it is copied, before a modification, When an ''array'' is assigned to a new reference, and it is copied, before a modification,
-due to the copy-on-write behavior, it will result in an array that is identical in any way +due to the copy-on-write behavior, it will result in an ''array'' that is identical in any 
-to the original one; in factthe copy also covers the "auto-increment" value:+way to the original one, inclusive of its "auto-increment" value:
  
 <PHP> <PHP>
Line 41: Line 44:
 </PHP> </PHP>
  
-This behavior happens, unfortunately, also between different scopes. Our code can receive + 
-"broken" ''array''-lists from third-parties that only appear to be well-indexed +This happens also between different function scopes. Our functions can receive "broken" 
-array-lists, but that in reality are not, because they were misused during their lifetime +''array''-lists from third-parties that only appear to be well-indexed, but that in 
-(for example, it was used ''unset()'' on them, instead of ''array_pop()'').+reality are not, because they were misused during their lifetime (classic example, it was 
 +used ''unset($array[$lastIndex])'' on them, instead of ''array_pop($array)'').
  
 As result of that, despite "copy on write", the value-type semantics, and even a different As result of that, despite "copy on write", the value-type semantics, and even a different
Line 68: Line 72:
 write", in order to guarantee a deterministic behavior to foreign scopes especially. The write", in order to guarantee a deterministic behavior to foreign scopes especially. The
 "auto-increment" value of the new variable reference must be equivalent to the "auto-increment" value of the new variable reference must be equivalent to the
-"auto-increment" value that the array would have if it was re-created entry by entry,+"auto-increment" value that the ''array'' would have if it was re-created entry by entry,
 as follows: as follows:
  
Line 90: Line 94:
 ===== 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 "auto-increment" value being 
-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 changeit offers protection against 
-protection against ''array''-lists that were misused with ''unset()'' instead of +''array''-lists that were misused with ''unset()'' instead of ''array_pop/_splice/_shift'' 
-''array_pop/_splice/_shift''.+and thus will only affect code that is already a candidate for improvements. Furthermore, 
 +the "auto-increment" value is copied inconsistently, when the ''array'' is empty: 
 + 
 +<PHP> 
 +$a = [0, 1]; 
 +unset($a[1]); 
 +$b = $a; 
 +$b[] = 2; 
 +// $b is [0 => 0, 2 => 2] 
 + 
 +$a = [0, 1]; 
 +unset($a[0], $a[1]); 
 +$b = $a; 
 +$b[] = 2; 
 +// $b is [0 => 2], rather than [2 => 2] 
 +</PHP> 
 + 
 +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 104: Line 125:
 Vote will require 2/3 majority Vote will require 2/3 majority
  
 +===== References  =====
 +
 +  * [[https://externals.io/message/105992|Pre-vote discussion on externals.io]]
rfc/normalize-array-auto-increment-on-copy-on-write.1560984053.txt.gz · Last modified: 2019/06/19 22:40 by wesnetmo