Good evening,
I have created an RFC containing two proposals.
The first is a less cumbersome syntax for arrays. At present, string keys in arrays are declared like this:
“stringKey” => 3,
That’s a whole five characters, seven including spaces, for your most common case (identifier-like key). 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,
Now it’s only two characters, three including spaces, for the most common case. It makes declaring arrays more convenient.
The second proposal is a less cumbersome syntax for dereferencing arrays with string keys. At present, you need to do this:
$myArray[“foobar”][“andSoOn”][“moreKeys”][“etc”]
That’s four characters per dereference. Yet for objects, only two are needed:
$myObject->foobar->andSoOn->moreProps->etc
Thus, this RFC also proposes the following syntax for dereferencing arrays with string keys, where said keys fit the profile of IS_STRING:
$myArray:>foobar:>andSoOn:>moreProps:>etc
The :> syntax can be considered provisional, I’m open to better alternatives.
The two proposals will be voted on separately, but I’m making a single RFC as they’re related and complement each other.
The RFC is here: https://wiki.php.net/rfc/bare_name_arrays
Please read it and tell me your thoughts. I already have a completely working patch with tests available.
Thanks!
--
Andrea Faulds
http://ajf.me/
Hi!
That’s a whole five characters, seven including spaces, for your most
common case (identifier-like key). 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 already do stringKey => 3 :) And saving characters is not really
a big priority. Changing language syntax and making millions of people
to learn new syntax just to save a couple of chars does not look like a
big win to me.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
You can already do stringKey => 3 :)
Yes, but then you’ll get:
Notice: Use of undefined constant stringKey - assumed 'stringKey' in Command line code on line 1
And saving characters is not really
a big priority.
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.
Array literals with string keys and dereferencing arrays with string keys are extremely common in PHP. So I think this reduction would benefit most users, especially those who deal with JSON and the like (which I do a lot in my on code), as this makes declaring arrays to be serialised more convenient. Also, foobar: just looks nicer than “foobar” =>, especially when you have syntax highlighting.
Changing language syntax and making millions of people
to learn new syntax just to save a couple of chars does not look like a
big win to me.
We did it for array(), I think we should do it again. Also, yes, millions might have to learn the new syntax, but I think it is simple and intuitive. It is not difficult to learn, and hey, perhaps millions can benefit, especially since both target very common cases (I expect they’re the most common, except for numerical indexes) when dealing with arrays.
Andrea Faulds
http://ajf.me/
This causes a huge backward incompatibility:
define('KEY', 'foo');
$data = array(KEY => 'bar');
Should $data have 'foo' or 'KEY' as element? AFAIK, some frameworks use
this constants in array keys.
You can already do stringKey => 3 :)
Yes, but then you’ll get:
Notice: Use of undefined constant stringKey - assumed 'stringKey'
in Command line code on line 1And saving characters is not really
a big priority.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.Array literals with string keys and dereferencing arrays with string keys
are extremely common in PHP. So I think this reduction would benefit most
users, especially those who deal with JSON and the like (which I do a lot
in my on code), as this makes declaring arrays to be serialised more
convenient. Also, foobar: just looks nicer than “foobar” =>, especially
when you have syntax highlighting.Changing language syntax and making millions of people
to learn new syntax just to save a couple of chars does not look like a
big win to me.We did it for array(), I think we should do it again. Also, yes, millions
might have to learn the new syntax, but I think it is simple and intuitive.
It is not difficult to learn, and hey, perhaps millions can benefit,
especially since both target very common cases (I expect they’re the most
common, except for numerical indexes) when dealing with arrays.Andrea Faulds
http://ajf.me/
This causes a huge backward incompatibility:
define('KEY', 'foo');
$data = array(KEY => 'bar');Should $data have 'foo' or 'KEY' as element? AFAIK, some frameworks use this constants in array keys.
‘foo’, I checked. Frameworks doing that should really not be, it’s really bad practise to rely on PHP’s fallback for non-existent constants, and that generates a Notice.
Now you’d have the option of doing array(KEY: ‘bar’) though, which does not resolve the constant.
Andrea Faulds
http://ajf.me/
You missed my point, I'm not saying about undefined constants, I'm saying
about the usage of defined constants as array keys. If the array
declaration don't expand constants to strings anymore, many code will
break. If we expand constants within array declaration, someone can mess up
the code by adding a define()
somewhere above and don't even realize what's
wrong (constants have global scope).
This causes a huge backward incompatibility:
define('KEY', 'foo');
$data = array(KEY => 'bar');Should $data have 'foo' or 'KEY' as element? AFAIK, some frameworks use
this constants in array keys.‘foo’, I checked. Frameworks doing that should really not be, it’s really
bad practise to rely on PHP’s fallback for non-existent constants, and that
generates a Notice.Now you’d have the option of doing array(KEY: ‘bar’) though, which does
not resolve the constant.Andrea Faulds
http://ajf.me/
You missed my point, I'm not saying about undefined constants, I'm saying
about the usage of defined constants as array keys. If the array
declaration don't expand constants to strings anymore, many code will
break.
I haven’t changed that. => still works the same as ever. I’m adding a colon syntax which takes a bare name instead of an expression.
If we expand constants within array declaration, someone can mess up
the code by adding adefine()
somewhere above and don't even realize what's
wrong (constants have global scope).
But that’s how it works at present. People writing array(foo => 3) and assuming that’s the same as array(‘foo’ => 3) need to try turning display_errors on.
Andrea Faulds
http://ajf.me/
Sorry, I didn't realize that this would not change the current behaviour.
Should be possible to mix both declarations?
define('FOO', 'bar');
$data = array(FOO => 'bar', FOO :> 'baz');
You missed my point, I'm not saying about undefined constants, I'm saying
about the usage of defined constants as array keys. If the array
declaration don't expand constants to strings anymore, many code will
break.I haven’t changed that. => still works the same as ever. I’m adding a
colon syntax which takes a bare name instead of an expression.If we expand constants within array declaration, someone can mess up
the code by adding adefine()
somewhere above and don't even realize
what's
wrong (constants have global scope).But that’s how it works at present. People writing array(foo => 3) and
assuming that’s the same as array(‘foo’ => 3) need to try turning
display_errors on.Andrea Faulds
http://ajf.me/
Sorry, I didn't realize that this would not change the current behaviour.
Should be possible to mix both declarations?
define('FOO', 'bar');
$data = array(FOO => 'bar', FOO :> 'baz’);
That would be array(FOO => ‘bar’, FOO: ‘baz’); since :> wouldn’t be used in the literal. But yes, you could mix them. That might be a little confusing, though luckily the intended spacing makes the difference more obvious (FOO => vs FOO:)
--
Andrea Faulds
http://ajf.me/
Hi!
Well, we already replaced array() with [].
That was readability improvement. But it's not a license to make the
language unreadable without any real gain in readability.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
Well, we already replaced array() with [].
That was readability improvement. But it's not a license to make the
language unreadable without any real gain in readability.
I don’t think readability would be hurt. [ key: value ] is as easy to understand as [ “key” => value ], and likely very familiar to people who have dealt with JavaScript or JSON. The :> operator does not look too far off from -> or =>, too, though again, I’m willing to use a better alternative if one exists.
It also happens to be the C99 digraph for ], take that as you will. :)
--
Andrea Faulds
http://ajf.me/
You can already do stringKey => 3 :)
Yes, but then you’ll get:
Notice: Use of undefined constant stringKey - assumed 'stringKey'
in Command line code on line 1And saving characters is not really
a big priority.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.Array literals with string keys and dereferencing arrays with string keys
are extremely common in PHP. So I think this reduction would benefit most
users, especially those who deal with JSON and the like (which I do a lot
in my on code), as this makes declaring arrays to be serialised more
convenient. Also, foobar: just looks nicer than “foobar” =>, especially
when you have syntax highlighting.
originally JSON(or what it ended up as JSON) didn't require the keys to be
quoted, but it didn't took too long to bump into a reserved keyword and
realize that it is better to require the keys to be quoted:
https://www.youtube.com/watch?v=-C-JoyNuQJs#3m30s
--
Ferenc Kovács
@Tyr43l - http://tyrael.hu
Good evening,
I have created an RFC containing two proposals.
This should be two RFCs then. Two proposals should be two RFCs.
Good evening,
I have created an RFC containing two proposals.
This should be two RFCs then. Two proposals should be two RFCs.
After thinking about it, I’ve split it into two RFCs:
However, for the sake of avoiding fragmentation, I think it would be best not to make two new mailing list threads.
--
Andrea Faulds
http://ajf.me/
Good evening,
I have created an RFC containing two proposals.
The first is a less cumbersome syntax for arrays. At present, string
keys in arrays are declared like this:“stringKey” => 3,
That’s a whole five characters, seven including spaces, for your most
common case (identifier-like key). 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,
We already voted no for this when discussing the short array syntax. See
the vote under "Array short syntax" at
https://wiki.php.net/todo/php54/vote
cheers,
Derick
--
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine
We already voted no for this when discussing the short array syntax. See
the vote under "Array short syntax" at
https://wiki.php.net/todo/php54/vote
That proposal was similar, but not quite the same. That added a : operator that functioned exactly the same as =>. This proposes a : operator that takes a bare name.
—
Andrea Faulds
http://ajf.me/
Good evening,
I have created an RFC containing two proposals.
The first is a less cumbersome syntax for arrays. At present, string
keys in arrays are declared like this:“stringKey” => 3,
That’s a whole five characters, seven including spaces, for your most
common case (identifier-like key). 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,
Now it’s only two characters, three including spaces, for the most
common case. It makes declaring arrays more convenient.
I don't believe in "less characters is better". I consider having two
syntaxes which work notably different confusing.
const FOO = 23;
$a = [
FOO => 42,
FOO: 3.14152
];
will lead to quite confusing results. We shouldn't go there imo.
The second proposal is a less cumbersome syntax for dereferencing
arrays with string keys. At present, you need to do this:$myArray[“foobar”][“andSoOn”][“moreKeys”][“etc”]
That’s four characters per dereference. Yet for objects, only two are
needed:$myObject->foobar->andSoOn->moreProps->etc
Thus, this RFC also proposes the following syntax for dereferencing
arrays with string keys, where said keys fit the profile of IS_STRING:$myArray:>foobar:>andSoOn:>moreProps:>etc
The :> syntax can be considered provisional, I’m open to better
alternatives.
Same as above - the benefit is minimal and might be easily misleading.
johannes
I'm personally against it adding :>
It's just pointles and kind'a looks like an object access.
Not to mention that one time access that deep is normal. but if you are
looping through a lot of data - it's quite an overhead and encoruaging
users to do it that way is just plain wrong to me.
As to the 'stringKey' : 3 - don't realy have anything against it, just the
readability is not that great when you put it in on larger array
definitions. => is very clear about what you are looking at and what it
does and is very visible. So, personaly I would prefer to have just one way
of defining key => value in PHP code.
I'm personally against it adding :>
It's just pointles and kind'a looks like an object access.
Not to mention that one time access that deep is normal. but if you are looping through a lot of data - it's quite an overhead and encoruaging users to do it that way is just plain wrong to me.
I don’t see how deep access is encouraged by :> any more than by ->
--
Andrea Faulds
http://ajf.me/
Where the key fits the profile of IS_STRING, this RFC proposes that you can do this instead:
stringKey: 3,
Now it’s only two characters, three including spaces, for the most common case. It makes declaring arrays more convenient.
I should note that an added bonus of passing this RFC would be that it would clear up what syntax to use for named parameters. Currently, syntax is up for debate.[0] I’m unhappy with test(foo => “bar”) because that’s inconsistent with arrays which do not quote bare word names with =>, and I think test($foo => “bar”) would be confusing. By adding the bare word colon syntax, there’d be a clear syntax that’s the same for both named parameters and arrays, if or when named parameters are implemented:
foobar(4, 7, foo: 3, bar: true, “parameter with spaces” => “baz”);
// Would be the same as
$myParams = [4, 7, foo: 3, bar: true, “parameter with spaces” => “baz”);
call_user_func_array(‘foobar’, $myParams);
[0]https://wiki.php.net/rfc/named_params#syntax
Andrea Faulds
http://ajf.me/
I don't like it because I think it reduces the readability of array
definitions too much.
My preference is syntax that makes it easy for me to convey the meaning of
my code to YOU, at a glance. In general, this means consistency. Typing a
couple of extra characters for the sake of readability is perfectly
reasonable to me.
As far as the, "we did that, so we should do this" argument I've been seeing
a lot: Your feature should stand on its own within the ENTIRE context of the
language. Many mistakes have been made in the lifetime of PHP. Don't point
at any one feature and say that it's a good reason to include this feature.
Bryan
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,
Now it’s only two characters, three including spaces, for the most common case. It makes declaring arrays more convenient.
Something I’m considering adding to the RFC is support for quoted names, such that you could do this:
[“stringKey”: 3]
Now, obviously, you can already do the same with the double arrow:
[“stringKey” => 3]
If that’s the case, then what’s the advantage? Well, it is even closer to JSON and JavaScript now than before. By adding this, we’d have the bareName: and “quoted name”: syntaxes established in arrays, and PHP developers would become familiar with them. (Incidentally, Apple’s new Swift language announced a few hours ago uses [“key”: 3] for their hash map literals, though that’s a bit off-topic.)
With that groundwork laid, a new RFC could be created proposing object literals with identical syntax to JSON’s objects, meaning you could paste JSON into PHP and (unless it had dollar signs) it would be perfectly valid PHP code and parse as you expect. It would support the “quoted key”: and unquotedKey: formats, but none other, thus aligning itself with JavaScript (and hence JSON, which uses “quoted key”: exclusively). That would also be useful for creating disposable objects. At present, the closest thing we have is immediately casting arrays to objects, which is tedious, as well as acting weirdly in some situations (integer keys in particular).
The JSON stuff is not, however, part of this RFC. It’s a separate RFC and thing I would implement if a quoted name syntax was added to this RFC and it passed. Also, I haven’t yet added “quoted key”: syntax, but I’m considering it.
--
Andrea Faulds
http://ajf.me/