rfc:deprecate_curly_braces_array_access
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:deprecate_curly_braces_array_access [2019/03/17 08:10] – rjhdby | rfc:deprecate_curly_braces_array_access [2019/07/19 15:30] – Add link to voting discussion thread theodorejb | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Deprecate curly braces | + | ====== PHP RFC: Deprecate curly brace syntax for accessing array elements and string |
- | * Version: 0.9 | + | |
* Date: 2019-03-12 | * Date: 2019-03-12 | ||
- | * Author: Andrey Gromov, andrewgrom@rambler.ru | + | * Author: Andrey Gromov |
- | * Status: | + | * Status: |
- | * First Published at: https:// | + | |
* Discussion: https:// | * Discussion: https:// | ||
+ | * Target version: PHP 7.4 | ||
+ | * Implementation: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | < | + | PHP allows both square brackets and curly braces |
+ | for accessing array elements | ||
- | ===== Proposal ===== | + | <code php> |
- | Deprecate curly braces syntax for accessing | + | $array |
+ | echo $array[1]; // prints 2 | ||
+ | echo $array{1}; // also prints 2 | ||
- | Reasons to deprecate curly braces | + | $string = " |
- | - Two ways to do the same thing. | + | echo $string[0]; // prints " |
- | - It is very rarely used nowadays. | + | echo $string{0}; // also prints " |
- | - It is almost not documented. There is only two short " | + | </ |
- | - Also, this syntax has reduced functionality. | + | |
- | - Deprecation and following removal will free this syntax for other features. For example: array/ | + | However, supporting both of these syntaxes can be confusing. Are there |
+ | circumstances where one syntax | ||
+ | there a difference in performance between them? Is there some difference | ||
+ | in scoping, since curly braces are the standard way to separate scope? | ||
+ | What' | ||
+ | |||
+ | Apart from two short notes in the PHP Manual, the curly brace syntax | ||
+ | virtually undocumented. Furthermore, | ||
+ | compared to the normal bracket syntax. For example, | ||
+ | pushing | ||
- | --------------------- | ||
<code php> | <code php> | ||
- | <?php | + | $array[] |
- | $arr=[1,2,3]; | + | echo $array[2]; // prints 3 |
+ | $array{} = 3; // Parse error: syntax error, unexpected ' | ||
+ | </ | ||
+ | |||
+ | Nor can it be used to create an array: | ||
+ | |||
+ | <code php> | ||
+ | $array = [1, 2]; // works | ||
+ | |||
+ | $array = {1, 2}; // Parse error: syntax error, unexpected ' | ||
+ | </ | ||
+ | |||
+ | It can't be used for list assignment, either: | ||
+ | |||
+ | <code php> | ||
+ | [$one, $two] = $array; // works | ||
+ | |||
+ | {$one, $two} = $array; // Parse error: syntax error, unexpected ',' | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Proposal ===== | ||
+ | Deprecate curly brace syntax for accessing array elements and string offsets. | ||
+ | |||
+ | <code php> | ||
+ | $arr = [1, 2, 3]; | ||
var_dump($arr{1}); | var_dump($arr{1}); | ||
</ | </ | ||
- | Output | + | Output: |
- | < | + | |
- | Warning: Array and string offset access syntax with curly braces is deprecated in / | + | < |
+ | Deprecated: Array and string offset access syntax with curly braces is deprecated in test.php line 3 | ||
int(2) | int(2) | ||
- | |||
</ | </ | ||
- | ===== Backward Incompatible Changes | + | ===== Discussion |
- | Yes. | + | ==== Wasn't the curly brace syntax deprecated once before? ==== |
+ | According to an internals discussion from June 2008 (see references | ||
+ | below), the curly brace syntax was deprecated in PHP 5.1 RC5, but the | ||
+ | deprecation warning was removed before the final release. In August | ||
+ | 2006, the documentation for '' | ||
+ | but again the deprecation never made it into a production release. | ||
- | ===== Proposed PHP Version(s) ===== | ||
- | PHP 7.4 | ||
- | ===== Future Scope ===== | + | ==== Is the curly brace syntax valuable for differentiating string and array offset access? |
- | Change to E_COMPILE_ERROR in PHP 8.0. | + | It has been suggested that the duplicate syntax is useful for differentiating |
- | Remove in PHP 8.1. | + | string and array offset access. The problem with this is that no distinction |
+ | is enforced by the language. Both syntaxes can be used for both arrays and | ||
+ | strings, so while one codebase might always use '' | ||
+ | '' | ||
+ | which leads to more confusion rather than less. | ||
- | ===== Voting ===== | + | To make sure that code is indexing a string and not an array, a type check |
- | 2/3 majority will be required. | + | should |
+ | and arrays (and thus doesn' | ||
- | ===== Patches and Tests ===== | ||
- | https:// | + | ==== How frequently is the curly brace syntax used? ==== |
+ | Nikita Popov checked the top 2k Composer packages, and found ~2.2k | ||
+ | individual uses of the curly brace array syntax. Compared to the 888.3k | ||
+ | total array accesses in the data set, usage of the alternative syntax is | ||
+ | about 0.25%. However, even this number is inflated somewhat due to | ||
+ | duplicate packages (for example, there are two packages that mirror the | ||
+ | WordPress Core repository, each with 182 usages). 92% of usages in the | ||
+ | top 2k packages are in just 25 unique projects. | ||
+ | |||
+ | |||
+ | ==== Will it be too much work for people to migrate code away from the curly brace syntax? ==== | ||
+ | A migration script has been implemented alongside the deprecation patch: | ||
+ | https://gist.github.com/theodorejb/763b83a43522b0fc1755a537663b1863 | ||
+ | |||
+ | |||
+ | ===== Backward Incompatible Changes ===== | ||
+ | A deprecation warning will be output when using the curly brace syntax | ||
+ | to access array or string offsets. | ||
+ | |||
+ | ===== Vote ===== | ||
+ | Started 3 July 2019. Ends 17th July 2019 | ||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ===== Future Scope ===== | ||
+ | Remove the feature entirely (replacing the deprecation warning | ||
+ | with a compiler error) in PHP 8 or another future release. | ||
+ | |||
+ | ===== References ===== | ||
+ | Current discussion: https:// | ||
- | Migration script. | + | Discussion about deprecation in June 2008: https://externals.io/message/38153. |
- | https://github.com/ | + | |
+ | Discussion about deprecation in November 2005: https:// | ||
rfc/deprecate_curly_braces_array_access.txt · Last modified: 2019/08/10 23:19 by derick