rfc:increment_decrement_fixes
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:increment_decrement_fixes [2020/03/01 17:41] – created 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 | silent | Error | Error | silent | | + | |
- | ^ object | 2 (+ Notice) | 2 (+ Notice) | unchanged | 0 (+ Notice) | 0 (+ Notice) | unchanged | | + | |
- | ^ resource#5 | 6 | 6 | resource#5 | 4 | 4 | resource #5 | | + | |
- | + | ||
- | + | ||
- | The reason this is possible is because < | + | |
Line 34: | Line 23: | ||
This behaviour has been raised as a bug at least three times ([[https:// | This behaviour has been raised as a bug at least three times ([[https:// | ||
- | 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--%% ^ | ||
+ | ^ Current ^ null | 1 | 1 | 1 | -1 | -1 | null | | ||
+ | ^ Proposed ^ null | 1 | 1 | 1 | -1 | -1 | -1 | | ||
===== Proposal 2: Incrementing and Decrementing Booleans ====== | ===== Proposal 2: Incrementing and Decrementing Booleans ====== | ||
- | The behaviour of booleans with most mathematical operations is to treat false as 0 and true as 1. However, the < | + | The behaviour of booleans with most mathematical operations is to treat false as 0 and true as 1. However, the < |
- | ^ Initial Value ^ %%$a = $a + 1%% ^ %%$a += 1%% ^ %%++$a%%, %%$a++%% ^ %%$a = $a - 1%% ^ %%$a -= 1%% ^ %%--$a%%, %%$a--%% ^ | + | This RFC proposes to change the behaviour so that it is in line with other mathematical contexts, as follows: |
- | ^ true | 2 | 2 | true | 0 | 0 | true | | + | |
- | ^ false | 1 | 1 | false | -1 | -1 | false | | + | |
- | This RFC proposes to change the behaviour so that it is in line with other mathematical contexts, giving this: | + | ^ ^ Initial Value ^ %%$a = $a + 1%% ^ %%$a += 1%% ^ %%++$a%%, %%$a++%% ^ %%$a = $a - 1%% ^ %%$a -= 1%% ^ %%--$a%%, %%$a--%% ^ |
+ | ^ Current ^ true | 2 | 2 | true | 0 | 0 | true | | ||
+ | ^ Proposed ^ true | 2 | 2 | 2 | 0 | 0 | 0 | | ||
+ | ^ Current ^ false | 1 | 1 | false | -1 | -1 | false | | ||
+ | ^ Proposed ^ false | 1 | 1 | 1 | -1 | -1 | -1 | | ||
- | ^ Initial Value ^ %%$a = $a + 1%% ^ %%$a += 1%% ^ %%++$a%%, %%$a++%% ^ %%$a = $a - 1%% ^ %%$a -= 1%% ^ %%--$a%%, %%$a--%% ^ | + | ===== Proposal 3: Error when incrementing or decrementing an array ===== |
- | ^ true | 2 | 2 | 2 | 0 | 0 | 0 | | + | |
- | ^ false | 1 | 1 | 1 | -1 | -1 | -1 | | + | Adding an integer to an array throws an Error with the message " |
+ | |||
+ | This RFC proposes to change the behaviour so that <php>$a=[]; $a++;</ | ||
+ | ^ ^ 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 67: | 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 93: | 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