PHP RFC: Bare Name Array Literal
At present, arrays are a tad cumbersome to define. String keys must be quoted and followed by a double arrow, then the element ends with a comma:
This means only 3 characters must be typed (4 including spaces) for the most common case, which is far more convenient. Hence, this RFC proposes an optional similar syntax for PHP, where keys do not need to be quoted when they are followed by a colon instead of a double arrow, so long as they fit the profile of IS_STRING:
This means that only 2 characters need to be typed (3 with spaces) for most key names. It makes defining arrays with string keys more convenient and it's easier to read (especially if you have an IDE which syntax highlights strings).
A secondary benefit is to the Named Parameters RFC in that it would provide a clear syntax choice consistent with arrays and the same as C# and Objective-C, i.e.:
Symmetry between arrays and named parameters would be desirable as otherwise it could cause confusion. This RFC would ensure it.
Even without named parameters, this makes it nicer to use functions which take an array of options. For example:
This makes creating JSON more convenient, too:
This complements the Bare Name Array Dereference RFC, as both this proposal and that one deal with using bare T_STRING names for array keys.
Along with the existing syntax for specifying a key-value pair in an array literal, the following new syntax is added:
T_STRING ":" value
This functions the same as a quoted key followed by the double arrow.
This does not affect using constants with the double arrow syntax, and the following code would still result in an array mapping 'bar' to 3:
By contrast, constants do not affect the colon syntax, and the following code would result in an array mapping 'foo' to 3:
The colon and double arrow syntaxes can be mixed if desired:
Backward Incompatible Changes
None. This will not affect existing code. ⇒ will continue to resolve constants. : will not.
Proposed PHP Version(s)
Proposed for the next PHP 5.x, which at the time of writing, is PHP 5.7.
SAPIs, extensions, opcache, constants and php.ini are unaffected. No new opcodes are added, this is purely a parsing change.
As aforementioned, it would give a better syntax for the Named Parameters RFC.
Unaffected PHP Functionality
Arrays still work the same as ever. [“foobar” ⇒ 3] and [foobar: 3] are exactly the same. It is completely optional and doesn't break anything.
Object literals might be a nice-to-have, but then again (object)[foobar: 3] would work just as well.
A 2/3 majority as it is a language change. Voting began on 2014-06-21 (after a hiccup it was restarted) and ended 2014-06-28.
Patches and Tests
There is a working and tested patch implementing this here: https://github.com/TazeTSchnitzel/php-src/compare/bareNameArrayLiteral
The branch on my GitHub account is here: https://github.com/TazeTSchnitzel/php-src/tree/bareNameArrayLiteral
If/when the RFC is implemented, this section should contain
- the version(s) it was merged to
- a link to the git commit(s)
- a link to the PHP manual entry for the feature
- The Named Parameters RFC is one reason for this
- http://phpsadness.com/sad/45 - one of my inspirations for this
- The Bare Name Array Dereference RFC goes hand-in-hand with this one
- http://marc.info/?t=140164578600002&r=1&w=2 - mailing list discussion on this RFC (it was originally a single RFC containing both this and the dereferencing proposal, but was split into two)
None as yet.