rfc:increment_decrement_fixes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:increment_decrement_fixes [2020/03/01 17:52] – imsop | rfc:increment_decrement_fixes [2020/03/01 21:08] – ready for discussion imsop | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Increment/ | ====== PHP RFC: Increment/ | ||
- | * Version: | + | * Version: 1.0 |
* Date: 2020-03-01 | * Date: 2020-03-01 | ||
- | * Author: Rowan Tommins, rowan.collins@gmail.com | + | * Author: Rowan Tommins |
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | The increment and decrement operators (< | + | The increment and decrement operators (< |
Intuitively, | Intuitively, | ||
- | This is currently not the case, as the table below shows: | + | This is currently not the case, because < |
- | + | For null and boolean, addition and subtraction | |
- | ^ Initial Value ^ %%$a = $a + 1%% ^ %%$a += 1%% ^ %%++$a%%, %%$a++%% ^ %%$a = $a - 1%% ^ %%$a -= 1%% ^ %%--$a%%, %%$a--%% ^ | + | |
- | ^ 0 | 1 | 1 | 1 | -1 | -1 | -1 | | + | |
- | ^ null | 1 | 1 | 1 | -1 | -1 | null | | + | |
- | ^ true | 2 | 2 | true | 0 | 0 | true | | + | |
- | ^ false | 1 | 1 | false | -1 | -1 | false | | + | |
- | ^ array | Error | Error | no effect | Error | Error | no effect | | + | |
- | ^ object | 2 (with Notice) | 2 (with Notice) | no effect | 0 (with Notice) | 0 (with Notice) | no effect | | + | |
- | ^ resource#5 | 6 | 6 | no effect | 4 | 4 | no effect | | + | |
- | + | ||
- | + | ||
- | The reason this is possible is because < | + | |
Line 36: | Line 25: | ||
Discrepancies in behaviour of null are particularly problematic, | Discrepancies in behaviour of null are particularly problematic, | ||
- | This RFC proposes to change the behaviour, so that < | + | This RFC proposes to change the behaviour, so that < |
^ ^ 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 53: | Line 42: | ||
^ Current ^ false | 1 | 1 | false | -1 | -1 | false | | ^ Current ^ false | 1 | 1 | false | -1 | -1 | false | | ||
^ Proposed ^ false | 1 | 1 | 1 | -1 | -1 | -1 | | ^ Proposed ^ false | 1 | 1 | 1 | -1 | -1 | -1 | | ||
+ | |||
+ | ===== Proposal 3: Error when incrementing or decrementing an array ===== | ||
+ | |||
+ | Adding an integer to an array throws an Error with the message " | ||
+ | |||
+ | This RFC proposes to change the behaviour so that < | ||
+ | |||
+ | ^ ^ Initial Value ^ %%$a = $a + 1%% ^ %%$a += 1%% ^ %%++$a%%, %%$a++%% ^ %%$a = $a - 1%% ^ %%$a -= 1%% ^ %%--$a%%, %%$a--%% ^ | ||
+ | ^ Current ^ any array | Error | Error | no effect | Error | Error | no effect | | ||
+ | ^ Proposed ^ any array | Error | Error | Error | Error | Error | Error | | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | The proposed changes | + | 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 68: | Line 67: | ||
===== Open Issues ===== | ===== Open Issues ===== | ||
- | objects, arrays, resources... | + | 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 < | + | **Undefined variables, array items, and object properties** are currently treated as null for most purposes, including the < |
- | * 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, | + | |
- | * Strings overload the < | + | 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, |
+ | |||
+ | **Strings** overload the < | ||
+ | |||
+ | **Objects and resources** are also left unchanged with < | ||
+ | |||
+ | 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--%% ^ | ||
+ | ^ 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#5 | 6 | 6 | no effect | 4 | 4 | no effect | | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | * Should decrementing null result in -1? (Yes / No) | + | The three proposed changes can be implemented independently, |
- | * Should incrementing and decrementing booleans act the same as addition and subtraction? | + | |
+ | | ||
+ | * Should incrementing and decrementing booleans act the same as addition and subtraction? (Yes / No) | ||
+ | * Should incrementing or decrementing an array throw an " | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | TODO | + | None yet. The implementation should be a simple addition to the '' |
===== Implementation ===== | ===== Implementation ===== | ||
Line 94: | Line 107: | ||
===== References ===== | ===== References ===== | ||
- | * [[https:// | + | |
- | * [[https:// | + | * [[https:// |
- | * [[https:// | + | * [[https:// |
===== Rejected Features ===== | ===== Rejected Features ===== | ||
TODO | TODO |
rfc/increment_decrement_fixes.txt · Last modified: 2022/02/18 14:38 by imsop