rfc:negative_array_index

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:negative_array_index [2017/04/21 19:04] pmmagarfc:negative_array_index [2020/08/01 23:40] (current) – RFC was implemented in both PHP 7.4 and PHP 8.0 carusogabriel
Line 1: Line 1:
 ====== PHP RFC: Arrays starting with a negative index ====== ====== PHP RFC: Arrays starting with a negative index ======
-  * Version: 0.1+  * Version: 0.4
   * Date: 2017-04-20   * Date: 2017-04-20
-  * Author: Pedro Magalhães, mail at pmmaga dot net +  * Author: Pedro Magalhães, pmmaga@php.net 
-  * Status: Draft+  * Status: Implemented
   * First Published at: http://wiki.php.net/rfc/negative_array_index   * First Published at: http://wiki.php.net/rfc/negative_array_index
  
 ===== Introduction ===== ===== Introduction =====
-When a developer adds an element to an array with a numeric key and later adds a new element to that array without specifying a key, the key of the new element depends on the sign of the first key. This RFC proposes to make this consistent by always using the value of the first key + 1 for the new element.+On the documentation for ''[[http://php.net/manual/en/function.array-fill.php|array_fill]]'' the ''start_index'' description mentions: "If ''start_index'' is negative, the first index of the returned array will be ''start_index'' and the following indices will start from zero.This is consistent everywhere when an explicit negative key is used and implicit keys are used afterwards.
  
 ===== Proposal ===== ===== Proposal =====
 In other words, any array that has a number ''n'' as it's first numeric key will have for it's next implicit key either ''n+1'' if ''n >= 0'' or ''0'' if ''n < 0''. This RFC proposes to make this consistent by always using ''n+1'' regardless of the sign of ''n''. In other words, any array that has a number ''n'' as it's first numeric key will have for it's next implicit key either ''n+1'' if ''n >= 0'' or ''0'' if ''n < 0''. This RFC proposes to make this consistent by always using ''n+1'' regardless of the sign of ''n''.
  
-As mentioned on the relevant part of the [[http://php.net/manual/en/language.types.array.php|arrays documentation]]:+As also mentioned on the relevant part of the [[http://php.net/manual/en/language.types.array.php|arrays documentation]]:
 > if no key is specified, the maximum of the existing integer indices is taken, and the new key will be that maximum value plus 1 (but at least 0). > if no key is specified, the maximum of the existing integer indices is taken, and the new key will be that maximum value plus 1 (but at least 0).
  
 This RFC proposes to eliminate the parenthesis part of that sentence. Which is an exception to a rule, that people have to learn one way or the other. This RFC proposes to eliminate the parenthesis part of that sentence. Which is an exception to a rule, that people have to learn one way or the other.
 +
 +This proposal targets 8.0. However, to ease the transition and find places where this may become an issue, there is a secondary vote for emitting a deprecation notice in cases where the behavior will change.
  
 All the variables in this code: All the variables in this code:
Line 43: Line 45:
 } }
 </code> </code>
 +
 +<blockquote>
 +NOTE: If accepted, during the deprecation phase the following ''E_DEPRECATED'' notice would be emitted in cases where the behavior will change:
 +
 +<code>
 +Deprecated: In the next major version of PHP the implicit keys of this array will start from -1 instead of 0 in ...
 +</code>
 +</blockquote>
  
 With the implementation of this RFC, they will all result in: With the implementation of this RFC, they will all result in:
Line 63: Line 73:
 <?php <?php
  
-$a[-2] = true; +$a[-2] = true; // Current: Key is -2, RFC: Key is -2 
-$a[] = true; +$a[] = true; // Current: Key is 0, RFC: Key is -1 
-$a[] = true;+$a[] = true; // Current: Key is 1, RFC: Key is 0
  
 if ($a[1] === true) { if ($a[1] === true) {
-    echo 'Save the world.';+    echo 'Accessing key 1 explicitly';
 } }
 </code> </code>
  
-Will no longer save the world.+Will no longer output ''Accessing key 1 explicitly''
 + 
 +<blockquote> 
 +NOTE: If accepted, during the deprecation phase the ''E_DEPRECATED'' notice mentioned in the previous section would be emitted. 
 +</blockquote>
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
-PHP 7.2+PHP 8.0 
 +(Deprecation notice for 7.3)
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
-Arrays with explicit keys, string keys, or an initial numeric index ''>0'' are not affected. This also means that arrays that never use explicit keys will still start with ''0''.+Arrays with explicit keys, string keys, or an initial numeric index ''>-1'' are not affected. This also means that arrays that never use explicit keys will still start with ''0''.
  
 Furthermore, iterating over arrays without explicit keys (ie. ''foreach'') is not affected. Furthermore, iterating over arrays without explicit keys (ie. ''foreach'') is not affected.
  
-===== Proposed Voting Choices ===== +===== Voting Choices ===== 
-This RFC requires a 2/3 majority.+Both votes require a 2/3 majority. 
 + 
 +<doodle title="Arrays starting with a negative index for 8.0" auth="pmmaga" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle> 
 + 
 +---- 
 + 
 +<doodle title="Deprecation notice for 7.3" auth="pmmaga" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle> 
 + 
 +<blockquote>The previous vote targeting 7.2 was closed with 14 for and 16 against</blockquote>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 91: Line 120:
  
 ===== References ===== ===== References =====
-Discussion on the mailing list:https://externals.io/thread/712+Version 0.3: 
 + 
 +Discussion on the mailing list: https://externals.io/message/98302 
 + 
 +First proposal Voting period discussion: https://externals.io/message/99412 
 + 
 +Second proposal Voting period discussion: https://externals.io/message/99511 
 + 
 +---- 
 +Version 0.4:
  
 +Discussion on the mailing list: https://externals.io/message/101861
rfc/negative_array_index.1492801480.txt.gz · Last modified: 2017/09/22 13:28 (external edit)