Re: [RFC] Bare Name Array
| From: | Rowan Collins | Date: | Sun, 01 Jun 2014 19:24:28 +0000 |
| Subject: | Re: [RFC] Bare Name Array | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-74675@lists.php.net to get a copy of this message | ||
On 01/06/2014 18:59, Andrea Faulds wrote:
In JavaScript, you can avoid quoting the key for this common case. I propose something similar for PHP. Where the key fits the profile of IS_STRING, this RFC proposes that you can do this instead: stringKey: 3,You can avoid quoting keys in JS *unless they are reserved words*. The list of officially reserved words was originally rather long, although it was shortened a bit by the ECMA committee at some point. Personally, I try to always quote my object keys, because I'm fed up of IE stopping all scripts on the page because someone did something like `var options = { default: 'oops, illegal bare key name!' };` As others have pointed out, PHP has an additional burden here that *all* barewords in an array literal are currently constant references.
Well, we already replaced array() with []. I think it is reasonable to consider saving characters in other places. It’s not a*big* priority, no, and we shouldn’t do it excessively, but if common cases are cumbersome, we should try to deal with them.When that syntax was introduced, that would have been the time to make the quotes optional. I don't know if it was explicitly discussed at the time, but it wasn't implemented. People are still getting used to the "new" syntax (5.4 is pretty recent in historical terms) so adding yet another way of writing the same thing, with a different version requirement, would get pretty confusing, even if there weren't good reasons against it per se. The proposed syntax is also a lot more JSON-like than the current one. This may seem attractive, but I think it's actually a bad idea, because it doesn't actually represent the same data type: JSON's "object" type (being, effectively, a data-only JS object) is an *unordered* key-value store, whereas PHP's associative arrays have independent keys and order. The short array-dereference syntax, on the other hand, is not an idea I've seen before, and I can't immediately think of a strong reason against it, other than that it had never occurred to me that it was necessary. Regards, -- Rowan Collins [IMSoP]