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/25 08:49] 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 and 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: Under Discussion +  * 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   * Discussion: https://externals.io/message/104744
 +  * Targets: PHP 7.4
 +  * Implementation: https://github.com/php/php-src/pull/4416
  
 ===== 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 
 + 
 +$string "foo"; 
 +echo $string[0]; // prints "f" 
 +echo $string{0}; // also prints "f" 
 +</code> 
 + 
 +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's the purpose of the curly brace syntax?
  
-Reasons to deprecate curly braces syntax:: +Apart from two short notes in the PHP Manual, the curly brace syntax is 
-  - Two ways to do the same thing. +virtually undocumentedFurthermore, it has reduced functionality 
-  - It is very rarely used nowadays. +compared to the normal bracket syntaxFor example, it cannot be used for 
-  - It is almost not documentedThere is only two short "NOTE" about it+pushing an element into an array:
-  - Also, this 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. +
-  - Furthermore, curly braces is standard way to separate scope in almost all languages including PHP (except one very rare and special case).+
  
---------------------- 
 <code php> <code php>
-<?php +$array[] 3; 
-$arr=[1,2,3];+echo $array[2]; // prints 3
  
-var_dump($arr{1});+$array{} = 3// Parse error: syntax error, unexpected '}'
 </code> </code>
  
-Output +Nor can it be used to create an array:
-<code>+
  
-Warning: Array and string offset access syntax with curly braces is deprecated in /root/php-src/1.php on line 4 +<code php
-int(2)+$array = [12]; // works
  
 +$array = {1, 2}; // Parse error: syntax error, unexpected '{'
 </code> </code>
  
-===== Discussion =====+It can't be used for list assignment, either:
  
-== Disagree == +<code php> 
-<blockquote>Do I understand it rightthat you're proposing deprecating accessing $string{$offset}? I think that's an important way of differentiation between string and array offset access.+[$one, $two] = $array; // works
  
-I'd vote "yes" for splitting syntax on array and string offset accessbut as is-1.</blockquote> +{$one$two} = $array; // Parse errorsyntax error, unexpected ',' 
-<blockquote>I personally use it because I like to quickly tell if I am doing an operation on a string or array, it is eye candy and makes a lot of sense. I think if anything the two syntaxes should be decoupled instead.+</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.</blockquote>+===== Proposal ===== 
 +Deprecate curly brace syntax for accessing array elements and string offsets.
  
-== 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't make this distinction (I think the only time I saw this in recent years was inside some old PEAR code), it's not really a useful indicator. The rarity of its use also makes it rather confusing. While $foo[$x] is well established as an array or string offset (or for that matter, ArrayObject) access and will be recognized by any programmer coming from any number of programming languages, $foo{$x} certainly is not, and is a WTF moment for people who don't happen to personally use this syntax.+Output:
  
-I'd prefer to phase out this syntax entirely and not reuse it for any other purpose. Reusing syntax is generally a not so good idea, because it means that the same syntax has different meaning in different PHP version.</blockquote> +<code> 
-<blockquote>Four thoughts:+Deprecated: Array and string offset access syntax with curly braces is deprecated in test.php line 3 
 +int(2) 
 +</code>
  
-1. I cannot think of any reason to separate themIf you want to make sure you are indexing a stringdo a type checknot bake {} into only working on strings.+===== 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 RC5but the 
 +deprecation warning was removed before the final release. In August 
 +2006the documentation for ''$str{42}'' read "deprecated as of PHP 6", 
 +but again the deprecation never made it into a production release.
  
-2. While Kalle says pretty much every codebase they've seen lately has used {} for indexing, I have never seen a codebase that used it. 
  
-3In any case, usage of {} can be migrated to [] by a style fixerright?+==== 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 accessThe 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 arraysanother codebase might use the opposite convention, 
 +which leads to more confusion rather than less.
  
-4. Even if we deprecate {}I don'think we'd be in any hurry to remove it, though with an automatic fixer this seems doable, if we care to do it at all.</blockquote>+To make sure that code is indexing a string and not an arraya type check 
 +should be used instead of relying on syntax that can be used for both strings 
 +and arrays (and thus doesn'tell you anything about the underlying type).
  
-===== 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/theodorejb/763b83a43522b0fc1755a537663b1863
  
-===== Patches and Tests ===== 
  
-https://github.com/php/php-src/compare/PHP-7.4...rjhdby:deprecate_alternate_array_access+===== 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="Deprecate curly brace array and string offset syntax in PHP 7.4" auth="rjhdby" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle> 
 + 
 +===== 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://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.1553503795.txt.gz · Last modified: 2019/03/25 08:49 by rjhdby