rfc:increment_decrement_fixes

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
Next revisionBoth sides next revision
rfc:increment_decrement_fixes [2020/03/01 18:14] – propose array error; defer object and resource discussion imsoprfc:increment_decrement_fixes [2020/03/01 21:08] – ready for discussion imsop
Line 1: Line 1:
 ====== PHP RFC: Increment/Decrement Fixes ====== ====== PHP RFC: Increment/Decrement Fixes ======
-  * Version: 0.2+  * Version: 1.0
   * Date: 2020-03-01   * Date: 2020-03-01
-  * Author: Rowan Tommins, rowan.collins@gmail.com +  * Author: Rowan Tommins [IMSoP], rowan.collins@gmail.com 
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/increment_decrement_fixes   * First Published at: http://wiki.php.net/rfc/increment_decrement_fixes
  
 ===== Introduction ===== ===== Introduction =====
  
-The increment and decrement operators (<php>++$a</php>, <php>$a++</php>, <php>--$a</php>, <php>$a--</php>) mostly behave the same as adding 1 to the variable. However, because they are implemented separately, there are a handful of anomalies regarding non-integer values, which do are not incremented consistently with an integer cast. In particular, this RFC focuses on null, boolean, and array values.+The increment and decrement operators (<php>++$a</php>, <php>$a++</php>, <php>--$a</php>, <php>$a--</php>) mostly behave the same as adding 1 to the variable. However, because they are implemented separately, there are a handful of anomalies regarding non-integer values, whose behaviour is not consistent with an integer cast. In particular, this RFC focuses on null, boolean, and array values.
  
 Intuitively, the <php>++</php> and <php>--</php> operators are equivalent to "add 1 to variable" and "subtract 1 from variable", respectively. As such, it would be expected that <php>$a=$a+1</php>, <php>$a+=1</php>, and <php>++$a</php> would end up with the same value in <php>$a</php>; and likewise, <php>$a=$a-1</php>, <php>$a-=1</php>, and <php>--$a</php>. Intuitively, the <php>++</php> and <php>--</php> operators are equivalent to "add 1 to variable" and "subtract 1 from variable", respectively. As such, it would be expected that <php>$a=$a+1</php>, <php>$a+=1</php>, and <php>++$a</php> would end up with the same value in <php>$a</php>; and likewise, <php>$a=$a-1</php>, <php>$a-=1</php>, and <php>--$a</php>.
Line 25: Line 25:
 Discrepancies in behaviour of null are particularly problematic, since undefined variables, array items, and object properties are currently treated as having a value of null. Discrepancies in behaviour of null are particularly problematic, since undefined variables, array items, and object properties are currently treated as having a value of null.
  
-This RFC proposes to change the behaviour, so that <php>$a=null; $a--;</php> or <php>$a=null; --$a;</php> will result in $a holding -1. This brings it into line with all other mathematical operations, include <php>$a++</php>, which treat null as equivalent to 0.+This RFC proposes to change the behaviour, so that <php>$a=null; $a--;</php> or <php>$a=null; --$a;</php> will result in $a holding -1. This brings it into line with all other mathematical operations, including <php>$a++</php>, which treat null as equivalent to 0.
  
 ^ ^ Initial Value ^ %%$a = $a + 1%% ^ %%$a += 1%% ^ %%++$a%%, %%$a++%% ^ %%$a = $a - 1%% ^ %%$a -= 1%% ^ %%--$a%%, %%$a--%% ^ ^ ^ Initial Value ^ %%$a = $a + 1%% ^ %%$a += 1%% ^ %%++$a%%, %%$a++%% ^ %%$a = $a - 1%% ^ %%$a -= 1%% ^ %%--$a%%, %%$a--%% ^
Line 45: Line 45:
 ===== Proposal 3: Error when incrementing or decrementing an array ===== ===== Proposal 3: Error when incrementing or decrementing an array =====
  
-Adding an integer to an array produces an "Unsupported operand types" error, but incrementing or decrementing it leaves it unchanged, with no Notice, Warning, or Error.+Adding an integer to an array throws an Error with the message "Unsupported operand types"but incrementing or decrementing an array leaves it unchanged, with no Notice, Warning, or Error.
  
-This RFC proposed to change the behaviour so that <php>$a=[]; $a++;</php> and <php>$a=[]; $a--;</php> raise the same error as <php>$a = [] + 1;</php>.+This RFC proposes to change the behaviour so that <php>$a=[]; $a++;</php> (or <php>++$a</php>and <php>$a=[]; $a--;</php> (or <php>--$a</php>raise the same error as <php>$a = [] + 1;</php>.
  
 ^ ^ Initial Value ^ %%$a = $a + 1%% ^ %%$a += 1%% ^ %%++$a%%, %%$a++%% ^ %%$a = $a - 1%% ^ %%$a -= 1%% ^ %%--$a%%, %%$a--%% ^ ^ ^ Initial Value ^ %%$a = $a + 1%% ^ %%$a += 1%% ^ %%++$a%%, %%$a++%% ^ %%$a = $a - 1%% ^ %%$a -= 1%% ^ %%--$a%%, %%$a--%% ^
Line 55: Line 55:
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
  
-The proposed changes to null and boolean handling are not backwards compatible. The current behaviour is not particularly useful, so it is unlikely that code is deliberately relying on it.+All of the proposed changes are explicit breaks in compatibility. The justification is that the current behaviour is neither intuitive nor useful. It is unlikely that code is deliberately relying on it, and it is likely that users would expect the behaviour to be consistent with adding 1.
  
  
Line 67: Line 67:
 ===== Open Issues ===== ===== Open Issues =====
  
-None raised yet.+None at time of initial discussion.
  
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
  
-* Undefined variables, array items, and object properties are currently treated as null for most purposes, including the <php>++</php> and <php>--</php> operators. Changing this behaviour is out of scope of this RFC, and the behaviour of variables explicitly set to null would still need to be defined+**Undefined variables, array items, and object properties** are currently treated as null for most purposes, including the <php>++</php> and <php>--</php> operators. Changing this behaviour is out of scope of this RFC, and the behaviour of variables explicitly set to null would still need to be defined even without this.
-* Similarly, most mathematical contexts will coerce null and false to 0, and true to 1; this RFC does not seek to change any cases where that logic is followed. Changes to that general principle might render this RFC obsolete, but might require a period of deprecation, so consistency of the current approach may still be valuable. +
-* Strings overload the <php>++</php> and <php>--</php> operators with complex behaviour not discussed here. Improving this deserves its own discussion, so has been left out of this RFC. +
-* Objects and resources are currently left unchanged by <php>++</php> and <php>--</php>. However, their behaviour with normal addition and subtraction is not particularly intuitive, so this RFC doesn't propose to extend it to increment and decrement.+
  
-For reference, the current behaviour is as follows:+Similarly, most mathematical contexts will coerce null and false to 0, and true to 1; this RFC does not seek to change any cases where that logic is followed. Changes to that general principle might render this RFC obsolete, but might require a period of deprecation, so consistency within the current approach may still be valuable. 
 + 
 +**Strings** overload the <php>++</php> and <php>--</php> operators with complex behaviour not discussed here. Improving this deserves its own discussion, so has been left out of this RFC. 
 + 
 +**Objects and resources** are also left unchanged with <php>++</php> and <php>--</php>; it would be consistent with the aim of this RFC to propose harmonising those with their behaviour when adding 1. However, in both cases, the current behaviour is of dubious value, so it may be better to leave this to a separate RFC tackling the general issue, e.g. raising an Error in line with arrays. 
 + 
 +For reference, the current behaviour for most operations is that objects are coerced to int(1), and resources to their internal ID, giving this table:
  
 ^ Initial Value ^ %%$a = $a + 1%% ^ %%$a += 1%% ^ %%++$a%%, %%$a++%% ^ %%$a = $a - 1%% ^ %%$a -= 1%% ^ %%--$a%%, %%$a--%% ^ ^ Initial Value ^ %%$a = $a + 1%% ^ %%$a += 1%% ^ %%++$a%%, %%$a++%% ^ %%$a = $a - 1%% ^ %%$a -= 1%% ^ %%--$a%%, %%$a--%% ^
-^ any object | 2 (with Notice) | 2 (with Notice) | no effect | 0 (with Notice) | 0 (with Notice) | no effect |+^ any object | 2 (with E_NOTICE) | 2 (with E_NOTICE) | no effect | 0 (with E_NOTICE) | 0 (with E_NOTICE) | no effect |
 ^ resource#1 | 2 | 2 | no effect | 0 | 0 | no effect | ^ resource#1 | 2 | 2 | no effect | 0 | 0 | no effect |
 ^ resource#5 | 6 | 6 | no effect | 4 | 4 | no effect | ^ resource#5 | 6 | 6 | no effect | 4 | 4 | no effect |
Line 87: Line 90:
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
-* Should decrementing null result in -1? (Yes / No) +The three proposed changes can be implemented independently, so three separate votes are proposed, each requiring the standard 2/3 super-majority to be accepted: 
-* Should incrementing and decrementing booleans act the same as addition and subtraction? (Yes / No) + 
-* Should incrementing or decrementing an array throw an "Unsupported operand types" error? (Yes / No)+  * Should decrementing null result in -1? (Yes / No) 
 +  * Should incrementing and decrementing booleans act the same as addition and subtraction? (Yes / No) 
 +  * Should incrementing or decrementing an array throw an "Unsupported operand types" error? (Yes / No)
  
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
  
-The implementation should be a simple addition to the ''increment_function'' and ''decrement_function'' definitions in ''Zend/zend_operators.c''.+None yet. The implementation should be a simple addition to the ''increment_function'' and ''decrement_function'' definitions in ''Zend/zend_operators.c''.
  
 ===== Implementation ===== ===== Implementation =====
Line 102: Line 107:
 ===== References ===== ===== References =====
  
-* [[https://externals.io/message/108602|Pre-RFC discussion]] +  * [[https://externals.io/message/108602|Pre-RFC discussion]] 
-* [[https://wiki.php.net/rfc/alpanumeric_decrement|RFC: Alphanumeric Decrement]] (rejected) +  * [[https://wiki.php.net/rfc/alpanumeric_decrement|RFC: Alphanumeric Decrement]] (rejected) 
-* [[https://wiki.php.net/rfc/normalize_inc_dec|RFC: Normalize inc/dec]] (inactive)+  * [[https://wiki.php.net/rfc/normalize_inc_dec|RFC: Normalize inc/dec]] (inactive)
  
 ===== Rejected Features ===== ===== Rejected Features =====
  
 TODO TODO
rfc/increment_decrement_fixes.txt · Last modified: 2022/02/18 14:38 by imsop