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 revision | ||
rfc:deprecate_curly_braces_array_access [2019/03/25 10:44] – rjhdby | rfc:deprecate_curly_braces_array_access [2019/08/10 23:19] (current) – derick | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Deprecate curly braces | + | ====== PHP RFC: Deprecate curly brace syntax for accessing array elements |
- | * 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:// | ||
+ | * Targets: 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 | ||
+ | |||
+ | $string | ||
+ | echo $string[0]; // prints " | ||
+ | echo $string{0}; // also prints " | ||
+ | </ | ||
- | Reasons to deprecate curly braces | + | However, supporting both of these syntaxes can be confusing. Are there |
- | * Two ways to do same things. Curly braces style fully covered by square | + | circumstances where one syntax |
+ | there a difference in performance between them? Is there some difference | ||
+ | in scoping, since curly braces | ||
+ | What's the purpose of the curly brace syntax? | ||
- | ^ Square braces | + | Apart from two short notes in the PHP Manual, the curly brace syntax is |
- | | $var[$key] = $value | + | virtually undocumented. Furthermore, it has reduced functionality |
- | | $value = $var[$key] | + | compared to the normal bracket syntax. For example, it cannot be used for |
- | | $var[] = $value | + | pushing an element into an array: |
- | | $var = [1,2,3] | ---| | + | |
- | | [$var1, $var2] = $arr| ---| | + | |
- | + | ||
- | * Curly braces access style is confusing, because: | + | |
- | - It is very rarely | + | |
- | - It is almost not documented. There is only two short " | + | |
- | - Curly braces is standard way to separate scope in almost all languages including PHP. | + | |
- | --------------------- | ||
<code php> | <code php> | ||
- | <?php | + | $array[] |
- | $arr=[1,2,3]; | + | echo $array[2]; // prints 3 |
- | var_dump($arr{1}); | + | $array{} = 3; // Parse error: syntax error, unexpected ' |
</ | </ | ||
- | Output | + | Nor can it be used to create an array: |
- | < | + | |
- | Warning: Array and string offset access syntax with curly braces is deprecated in /root/php-src/1.php on line 4 | + | < |
- | int(2) | + | $array = [1, 2]; // works |
+ | $array = {1, 2}; // Parse error: syntax error, unexpected ' | ||
</ | </ | ||
- | ===== Discussion ===== | + | It can't be used for list assignment, either: |
- | == Disagree == | + | <code php> |
- | <blockquote>Do I understand it right, that you're proposing deprecating accessing | + | [$one, $two] = $array; // works |
- | I'd vote " | + | {$one, $two} = $array; // Parse error: syntax error, unexpected ',' |
- | < | + | </code> |
- | From a usage PoV, then from personal experience (outside my own projects), I have seen many usages of this syntax, even as late as my | ||
- | current company with a growing codebase that is nearing 1.1m LoC, I have seen this syntax used countless of times. In fact I don't think I | ||
- | have been working with a code base where I have not seen this syntax used. | ||
- | I have asked fellow developers around in my community today and they have no strong opinion on either, but do like the distinction between.</ | + | ===== Proposal ===== |
+ | Deprecate curly brace syntax for accessing array elements | ||
- | == Agree == | + | <code php> |
- | <blockquote>I'm okay with this. This syntax has already been deprecated once, though it was reverted for reasons I don't remember. | + | $arr = [1, 2, 3]; |
+ | var_dump($arr{1}); | ||
+ | </code> | ||
- | There are some people using this syntax to distinguish between array and string access. It's a nice thought, but as the vast majority of code doesn' | + | Output: |
- | I'd prefer to phase out this syntax entirely | + | < |
- | < | + | Deprecated: Array and string offset access |
+ | int(2) | ||
+ | </code> | ||
- | 1. I cannot think of any reason | + | ===== Discussion ===== |
+ | ==== Wasn't the curly brace syntax deprecated once before? ==== | ||
+ | According | ||
+ | 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. | ||
- | 2. While Kalle says pretty much every codebase they' | ||
- | 3. In any case, usage of {} can be migrated to [] by a style fixer, right? | + | ==== Is the curly brace syntax valuable for differentiating string and array offset access? ==== |
+ | It has been suggested that the duplicate syntax is useful for differentiating | ||
+ | string and array offset access. The problem with this is that no distinction | ||
+ | is enforced by the language. Both syntaxes | ||
+ | strings, so while one codebase might always use '' | ||
+ | '' | ||
+ | which leads to more confusion rather than less. | ||
- | 4. Even if we deprecate {}, I don' | + | To make sure that code is indexing a string and not an array, a type check |
+ | should be used instead of relying on syntax that can be used for both strings | ||
+ | and arrays (and thus doesn' | ||
- | ===== Backward Incompatible Changes ===== | ||
- | Yes. | ||
- | ===== Proposed PHP Version(s) ===== | + | ==== How frequently is the curly brace syntax used? ==== |
- | PHP 7.4 | + | 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. | ||
- | ===== Future Scope ===== | ||
- | Change to E_COMPILE_ERROR in PHP 8.0. | ||
- | Remove in PHP 8.1. | ||
- | ===== Voting ===== | + | ==== Will it be too much work for people to migrate code away from the curly brace syntax? |
- | 2/3 majority will be required. | + | A migration script has been implemented alongside the deprecation patch: |
+ | https://gist.github.com/ | ||
- | ===== Patches and Tests ===== | ||
- | https://github.com/php/php-src/compare/PHP-7.4...rjhdby: | + | ===== 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: | ||
- | 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.1553510659.txt.gz · Last modified: 2019/03/25 10:44 by rjhdby