rfc:bare_name_arrays

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:bare_name_arrays [2014/06/01 19:32]
ajf Clarity on constants
rfc:bare_name_arrays [2017/09/22 13:28] (current)
Line 1: Line 1:
-====== PHP RFC: Bare Name Arrays ====== +This was single RFC, but as it contained two proposals, it was split into two RFCs, [[rfc:bare_name_array_literal|Bare Name Array Literal]] and [[rfc:bare_name_array_dereference|Bare Name Array Dereference]].
-  * Version: 1.0.1 +
-  * Date: 2014-06-01 +
-  * Author: Andrea Faulds <ajf@ajf.me> +
-  * Status: Under Discussion +
-  * First Published at: http://wiki.php.net/rfc/bare_name_arrays +
- +
-===== Introduction ===== +
- +
-This RFC contains two proposals, the first dealing with array definition. At present, arrays are tad cumbersome to define. String keys must be quoted and followed by a double arrow, then the element ends with a comma: +
- +
-<code php> +
-$myArray = [ +
-    "stringKey" => 3, +
-    "otherKey" => true, +
-    "foobar" => [ +
-        "anotherKey" => false, +
-        "andSoOn" => [ +
-            "moreKeys" => "thing" +
-        ] +
-    ], +
-    "key with spaces" => "boo" +
-]; +
-</code> +
- +
-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: +
- +
-<code javascript> +
-var myArray = { +
-    stringKey: 3, +
-    otherKey: true, +
-    foobar: { +
-        anotherKey: false, +
-        andSoOn: { +
-            moreKeys: thing +
-        } +
-    }, +
-    "key with spaces": "boo" +
-}; +
-</code> +
- +
-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 PHPwhere 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: +
- +
-<code php> +
-$myArray = [ +
-    stringKey: 3, +
-    otherKey: true, +
-    foobar: [ +
-        anotherKey: false, +
-        andSoOn: [ +
-            moreKeys: thing +
-        ] +
-    ], +
-    "key with spaces" => "boo" +
-]; +
-</code> +
- +
-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: +
- +
-<code php> +
-$myArray['foobar']['andSoOn']['moreKeys'+
-</code> +
- +
-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: +
- +
-<code php> +
-$myObject->foobar->andSoOn->moreKeys +
-</code> +
- +
-That requires just two characters. Sothis RFC proposes a similar syntax for dereferencing string keys fitting the IS_STRING profile: +
- +
-<code php> +
-$myArray:>foobar:>andSoOn:>moreKeys +
-</code> +
- +
-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 literalthe 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: +
- +
-<code php> +
-const('foo', 'bar'); +
-$arr = foo => 3]; +
-</code> +
- +
-Similarly, constants do not affect : and the following code would result in an array mapping 'foo' to 3: +
- +
-<code php> +
-const('foo', 'bar'); +
-$arr = foo3]; +
-</code> +
- +
-==== 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" => 3and [foobar: 3are 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)[foobar3would 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  +
-  - the version(s) it was merged to +
-  - a link to the git commit(s) +
-  - 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)