rfc:bare_name_arrays

This is an old revision of the document!


PHP RFC: Bare Name Arrays

Introduction

This RFC contains two proposals, the first dealing with array definition. 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:

$myArray = [
    "stringKey" => 3,
    "otherKey" => true,
    "foobar" => [
        "anotherKey" => false,
        "andSoOn" => [
            "moreKeys" => "thing"
        ]
    ],
    "key with spaces" => "boo"
];

This adds up to a total of 5 characters that must be typed, 7 including spaces. However, in JavaScript, you do not need to quote key names if they fit the profile of a normal identifier (no spaces, doesn't begin with a digit, no dashes, etc.), making objects very convenient to define:

var myArray = {
    stringKey: 3,
    otherKey: true,
    foobar: {
        anotherKey: false,
        andSoOn: {
            moreKeys: thing
        }
    },
    "key with spaces": "boo"
};

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:

$myArray = [
    stringKey: 3,
    otherKey: true,
    foobar: [
        anotherKey: false,
        andSoOn: [
            moreKeys: thing
        ]
    ],
    "key with spaces" => "boo"
];

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

The second proposal in this RFC deals with array dereferencing, and complements the first proposal. The current array dereferencing syntax is also somewhat cumbersome:

$myArray['foobar']['andSoOn']['moreKeys']

To dereference an array with a string key requires four characters. By contrast, dereferencing objects is much shorter, as the key name does not need to be quoted most of the time:

$myObject->foobar->andSoOn->moreKeys

That requires just two characters. So, this RFC proposes a similar syntax for dereferencing string keys fitting the IS_STRING profile:

$myArray:>foobar:>andSoOn:>moreKeys

The :> syntax is just my initial proposal. If there is a better (less ugly?) alternative token, please suggest it.

The literal syntax proposal and the dereferencing proposal shall be voted on separately.

Proposal

Array literals

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 ⇒ and the following code would still result in an array mapping 'bar' to 3:

const('foo', 'bar');
$arr = [ foo => 3];

Similarly, constants do not affect : and the following code would result in an array mapping 'foo' to 3:

const('foo', 'bar');
$arr = [ foo: 3];

Array dereferencing

Along with the existing syntax for dereferencing a key in an array, the following new syntax is added:

  array :> T_STRING

This functions the same as dereferencing a quoted key in square brackets.

Backward Incompatible Changes

None. Both do not affect existing code.

Proposed PHP Version(s)

Proposed for the next PHP 5.x, which at the time of writing, is PHP 5.7.

RFC Impact

SAPIs, extensions, opcache, constants and php.ini are unaffected. No new opcodes are added, this is purely a parsing change.

Open Issues

The :> syntax might not be ideal, and I'm willing to take suggestions for better syntax.

Unaffected PHP Functionality

Arrays still work the same as ever. [“foobar” ⇒ 3] and [foobar: 3] are exactly the same, as are $arr:>foobar and $arr['foobar'], so both are completely optional and don't break anything.

Future Scope

Object literals might be a nice-to-have, but then again (object)[foobar: 3] would work just as well.

Proposed Voting Choices

There shall be two votes. The first on the : syntax in literals, the second on the :> syntax for dereferencing. Both require a 2/3 majority as they're language changes. They'll be straight Yes/No votes.

If other syntax options instead of :> are proposed, I might hold a vote on them.

Patches and Tests

There is a working and tested patch implementing both of these features separately here: https://github.com/TazeTSchnitzel/php-src/compare/bareNameArrays

The branch on my GitHub account is here: https://github.com/TazeTSchnitzel/php-src/tree/bareNameArrays

Implementation

If/when the RFC is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

* http://phpsadness.com/sad/45 - one of my inspirations for this

Rejected Features

None as yet.

rfc/bare_name_arrays.1401651168.txt.gz · Last modified: 2017/09/22 13:28 (external edit)