rfc:deprecate_curly_braces_array_access

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
rfc:deprecate_curly_braces_array_access [2019/03/15 13:30] rjhdbyrfc:deprecate_curly_braces_array_access [2019/08/10 23:19] (current) derick
Line 1: Line 1:
-====== PHP RFC: Deprecate curly braces syntax for accessing array element or string offset. ====== +====== PHP RFC: Deprecate curly brace syntax for accessing array elements and string offsets ======
-  * Version: 0.9+
   * Date: 2019-03-12   * Date: 2019-03-12
-  * Author: Andrey Gromovandrewgrom@rambler.ru +  * Author: Andrey Gromov <andrewgrom@rambler.ru>, Theodore Brown <theodorejb@outlook.com> 
-  * Status: Draft +  * Status: Implemented (in PHP 7.4) 
-  * First Published at: https://wiki.php.net/rfc/deprecate_curly_braces_array_access?do=edit +  * Discussion: https://externals.io/message/104744 
- +  * Targets: PHP 7.4 
-This is a suggested template for PHP Request for Comments (RFCs). Change this template to suit your RFC.  Not all RFCs need to be tightly specified.  Not all RFCs need all the sections below+  * Implementation: https://github.com/php/php-src/pull/4416
-Read https://wiki.php.net/rfc/howto carefully!+
  
 ===== Introduction ===== ===== Introduction =====
-<blockquote>Both square brackets and curly braces can be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} will both do the same thing in the example above).</blockquote>+PHP allows both square brackets and curly braces to be used interchangeably 
 +for accessing array elements and string offsetsFor example:
  
-===== Proposal ===== +<code php> 
-Deprecate curly braces syntax for accessing array element and string offset.+$array [1, 2]; 
 +echo $array[1]; // prints 2 
 +echo $array{1}; // also prints 2
  
-Reasons to deprecate curly braces syntax:: +$string = "foo"; 
-  - Two ways to do the same thing. +echo $string[0]; // prints "f" 
-  - It is very rarely used nowadays. +echo $string{0}; // also prints "f" 
-  - It is almost not documented. There is only two short "NOTE" about it. +</code> 
-  - Alsothis syntax has reduced functionality. You can't use it for pushing element into array "$arr{} = 1;", creating array "$a={1,2};" or in other similar cases. + 
-  - Deprecation and following removal will free this syntax for other features. For examplearray/string slice, absolute offset access, "windows" (like "slice" in GoLang), etc.+However, supporting both of these syntaxes can be confusing. Are there 
 +circumstances where one syntax behaves differently than the other? Is 
 +there a difference in performance between them? Is there some difference 
 +in scoping, since curly braces are the standard way to separate scope? 
 +What'the purpose of the curly brace syntax? 
 + 
 +Apart from two short notes in the PHP Manualthe curly brace syntax is 
 +virtually undocumented. Furthermore, it has reduced functionality 
 +compared to the normal bracket syntaxFor example, it cannot be used for 
 +pushing an element into an array:
  
---------------------- 
 <code php> <code php>
-<?php +$array[] 3; 
-$arr=[1,2,3];+echo $array[2]; // prints 3
  
 +$array{} = 3; // Parse error: syntax error, unexpected '}'
 +</code>
 +
 +Nor can it be used to create an array:
 +
 +<code php>
 +$array = [1, 2]; // works
 +
 +$array = {1, 2}; // Parse error: syntax error, unexpected '{'
 +</code>
 +
 +It can't be used for list assignment, either:
 +
 +<code php>
 +[$one, $two] = $array; // works
 +
 +{$one, $two} = $array; // Parse error: syntax error, unexpected ','
 +</code>
 +
 +
 +===== 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});
 </code> </code>
  
-Output +Output:
-<code>+
  
-Warning: Array and string offset access syntax with curly braces is deprecated in /root/php-src/1.php on line 4+<code> 
 +Deprecated: Array and string offset access syntax with curly braces is deprecated in test.php line 3
 int(2) int(2)
- 
 </code> </code>
 +
 +===== Discussion =====
 +==== 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 ''$str{42}'' read "deprecated as of PHP 6",
 +but again the deprecation never made it into a production release.
 +
 +
 +==== 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 can be used for both arrays and
 +strings, so while one codebase might always use ''$str[0]'' for strings and
 +''$arr{0}'' for arrays, another codebase might use the opposite convention,
 +which leads to more confusion rather than less.
 +
 +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't tell you anything about the underlying type).
 +
 +
 +==== 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 ===== ===== Backward Incompatible Changes =====
-Yes.+A deprecation warning will be output when using the curly brace syntax 
 +to access array or string offsets.
  
-===== Proposed PHP Version(s) ===== +===== Vote ===== 
-PHP 7.4+Started 3 July 2019. Ends 17th July 2019 
 +<doodle title="Deprecate curly brace array and string offset syntax in PHP 7.4" auth="rjhdby" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
 ===== Future Scope ===== ===== Future Scope =====
-Change to E_COMPILE_ERROR in PHP 8.0. +Remove the feature entirely (replacing the deprecation warning 
-Remove in PHP 8.1. +with a compiler error) in PHP 8 or another future release.
- +
-===== Patches and Tests =====+
  
-https://github.com/php/php-src/compare/PHP-7.4...rjhdby:deprecate_alternate_array_access+===== References ===== 
 +Current discussion: https://externals.io/message/104744 and https://externals.io/message/106130.
  
-Migration script. +Discussion about deprecation in June 2008: https://externals.io/message/38153.
-https://github.com/rjhdby/php-src/blob/deprecate_alternate_array_access/convert_array_access_braces.php+
  
 +Discussion about deprecation in November 2005: https://externals.io/message/20143.
  
rfc/deprecate_curly_braces_array_access.1552656612.txt.gz · Last modified: 2019/03/15 13:30 by rjhdby