rfc:deprecate_curly_braces_array_access

This is an old revision of the document!


PHP RFC: Deprecate curly braces syntax for accessing array element or string offset.

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. Read https://wiki.php.net/rfc/howto carefully!

Introduction

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).

Proposal

Deprecate curly braces syntax for accessing array element and string offset.

Reasons to deprecate curly braces syntax::

  1. Two ways to do the same thing.
  2. It is very rarely used nowadays.
  3. It is almost not documented. There is only two short “NOTE” about it.
  4. Also, this syntax have reduced functionality. You can't use it for pushing element into array “$arr{} = 1;”, creating array “$a={1,2};” or in other similar cases.
  5. Depreciation and following removal will free this syntax for other features. For example: array/string slice, absolute offset access, “windows” (like “slice” in GoLang), etc.

<?php
$arr=[1,2,3];
 
var_dump($arr{1});

Output

Warning: Array and string offset access syntax with curly braces is deprecated in /root/php-src/1.php on line 4
int(2)

Backward Incompatible Changes

Yes.

Proposed PHP Version(s)

PHP 7.4

Future Scope

Change to compile error in PHP 8.0 and completely remove this syntax in PHP 8.1+

Patches and Tests

https://github.com/php/php-src/compare/master...rjhdby:deprecate_curly_bracess_array_access

Not sure if it is good realisation. Also maybe need to change warning message.

rfc/deprecate_curly_braces_array_access.1552467111.txt.gz · Last modified: 2019/03/13 08:51 by rjhdby