Better array syntax for PHP: Here's your chance to weigh in
Posted on 9/6/08 by Nate Abele
Hi folks.
Recently there's been some discussion on the PHP internals mailing list about providing an alternate array declaration syntax for PHP.
Instead of:
it would be possible to do
Simpler, cleaner and very familiar to anyone who has done development in almost any other modern web language, especially JavaScript.
Well, there's a patch available, but 2/3 of the active PHP committers (who participated in the vote) voted against its inclusion. However, of the end-users participating in the discussion, 17 out of 20 voted in favor. Shortly before the voting was concluded, there was a call for some user-land input, so I decided to humbly submit my two pennies.
This fact has been pointed out previously, but it bears repeating: the
divide between users and committers on this issue is stark: only 1/3
of committers are in favor, but almost all users who have voiced
opinions on the issue are in favor. The people actually using the
language want something new, because in this case something new is
better. Come on guys, arrays are our bread and butter. PHP has more
support for working with arrays than it does for doing pretty much
anything else.
I can't imagine I'll be taken very seriously. I also find it hard to believe that many actual users of PHP would be against this, a fact which is all the more tragic given the rhetoric of the core committers who are against it (a full summary of the discussion can be seen at http://markmail.org/message/rsi4welftwou24p3, please note that this is not updated in real-time).
Fortunately PHP, like Cake, is an Open Source project, and even I've been known to implement and support features which I personally don't like or have no use for, if the community support behind the idea is strong enough. The same goes for PHP, so here's your chance to make your voice heard. Blog, leave comments, post to the internals list, lobby your local PHP core committer (remember to be nice).
Obviously a feature like this, even though we wouldn't be able to directly take advantage of it anytime soon, would be a great help in pushing all PHP projects, especially ours, forward to the future.
You can skip to the end and add a comment.
Agreed. Can't imagine a reason for not doing this.
Remains to be seen who is in favor of JS and who is not... :P
I would love to see this implemented, it seems so redundant to have to explicitly define it as an array. This syntax is elegant and functional.
Well, I also like this syntax and I would love to see it in PHP :)
I agree that this should be implemented. As Nate mentioned, there's so much array manipulation going on. Why not make it simpler?
It's just a logical follow-on from being able to do:
$array[] = 'Text';
and
$array['key'] = 'Other Text';
I don't quite understand the point of this. It's not like any new functionality would be achieved by this. I'm solely an end-user of PHP and I don't feel that saving a few characters or looking 'prettier' is really worth the cost of implementing and the potential for even very simple scripts to have compatibility issues with older versions of PHP.
PHP is already overloaded with syntax variations as it is. Adding another surely doesn't make things any simpler?
I agree with Ben. It's true that square bracket creation is more common, and more consistent with other access syntax. However, PHP is already bloated with inconsistent language and this won't help this - the existing syntax would still be supported. And it's not like typing array() is difficult or counter intuitive.
So while I don't see this proposal as a bad idea, I don't see it is worth championing. Dropping "->" for object accessors would get my vote - I can easily type the word "array()" much faster than "->", particularly when quotes are involved.
I too would love to see this. Everytime I have to type out array() a piece of me dies, and I wish for more elegant syntax found in other languages. Not only does javascript use this syntax but so do ruby and python.
So named arrays would look like:
$data = [
'id' => 1,
'name' => 'New',
'created' => '2008-06-10'
]; ??
Looks really nice, I think!
Love the email to php.internals Nate - Mental Note: "Must try that sort of approach next time I'm in trouble with the Cake team" :)
"Think of the children" is going to be my personal mantra...
I, for one, would love not only this syntax but a full-on JSON interpreter built in so that we could make associative arrays using curly-braces just like I can using JavaScript. Thus something like this...
$foo = array("bar"=>"baz");
... would become ...
$foo = { "bar": "baz" }
... or if the PHP => operator would be necessary ...
$foo = { "bar" => "baz" }
Personally, the idea of a language being bloated is a negative, and potentially destructive attitude. The ability to use a variety of syntax to reach an equivalent result creates a flexible language that can adapt to the needs of the users and, in some ways, the experience of the users. For someone coming from JavaScript, a JSON-like method by which to create arrays would be very familiar and reduce the barrier to programming PHP. How could this not be a good thing?
Useless! We already have too many ways to declare an array. Let's keep this synthax for a future use (see C#).
I completely agree with your statement -- just because somebody is not a committer does not automatically make his/her ideas invalid. Why a project would not listen to its users makes no sense to me.
@David Dashifen Kees:
$foo = json_decode('{ "bar": "baz" }');
S
As for the array argument: I'm pretty neutral. I don't mind typing a few extra characters.
S
@Fred:
How would reserving "$arr = ['', '']" for future use be relevant? I don't see any way this syntax could be used in other than array creation.
Personally I'm all for this! I've always felt typing in array() is redundant as () implies a function. PHP is full of bad naming conventions, this would at least be a step in the right direction.
I have my doubts about replacing '=>' with ':' though. This would be a complete over-haul and not that consistent.
I would like to use that syntax but even nicer would be things like $arr.append(). I guess PHP would have to go the full object-oriented route for that to happen but for now I'll just admire Python's list methods: http://docs.python.org/tut/node7.html#SECTION007100000000000000000
IMHO, this syntax does not add too much value to PHP or the PHP programming community. Syntax in every language is always going to be different; to those who want it so that it's simpler to learn - I have to say this is not really going to help. For those looking to make things simpler, I agree that this will be an attractive addition but creating one more alternative usage and adding it to the core language does not seem like the best approach.
I think it would be great to have this syntax enabled. I for one find myself more often then not pasting arrays between JS and PHP, and using similar syntax, would be nice!
While we are add it, I would love for PHP to include 1..10 style syntax as well.
Krudant: What would you suggest instead? Simply not including it?
Peter Robinett: Hrm soundsi nteresting, though PHP could easily become Python then, no? :]
It looks cool and all but how long will it be backwards compatible? My clients won't like to pay for expensive code grinding to upgrade their code because we don't like the aestetics of the array code.
This is a verry fundemental change in the way most php code is formulated and will have to be backwards compatible for a long, long verry long time. Change is good, unless your active in the corporate world and your company depends on a php driven application.
This addition would be huge.
Should also include the ability to handle associative arrays as mentioned above.
I like the idea of the square bracket arrays. However, I don't write core code for PHP, so I don't know what the maintenance would take.
I don't really see how changing from array('red','blue','green') to ['red','green','blue'] is anything to get excited about. in fact, i'd say that it's much easier at a glance to tell that the first is an array, because its stamped across its forehead
@Sean Coates & @Matthew Weier O'Phinney: Hi guys!
@Matthew Weier O'Phinney: Some seem to think that if we can get enough major PHP projects together in support of this idea, that we might be able to get it accepted into the core after all. Thoughts? One of the developers of Drupal has also come out in favor: http://drupal4hu.com/node/152.
To clarify about associative arrays, I don't think anybody is talking seriously about replacing => with : or using curly braces instead, so associative array contents would still look the same, i.e. ["foo" => "bar"].
@All The Sayers of Nay: No one would be forcing you to use this syntax, nor is there any discussion of removing or deprecating "array()", so please don't confuse the issue. For those who think this is redundant, that argument is also bogus: should we throw out a better solution just because we already have a worse one that works fine? That is the most regressive and destructive attitude you can possibly have, and it does nothing to further the growth and development of the language.
And need I remind you, there is precedent in JavaScript, which suffers from exactly zero of the same major issues as PHP (i.e. being horribly devoid of consistency), yet they also have two different ways of declaring arrays.
Also, I think the psychology surrounding this issue is interesting: very few people are vehemently against it. Most people who are against it simply think it's not that great an idea, but they're not going out and campaigning about it. The people who are for it, on the other hand, are really really for it, as you can see. I wonder if there's a correlation between the type of programmer you are, and your preference for better-looking, more concise code and syntax constructs. My hunch is that there's a correlation between that and how much you enjoy programming. ;-) But that's a topic for another post, I think.
One final note to the people who are against this issue: either way it is decided, you can't possibly lose. If this decision gets shot down again, the status quo is maintained. If by some miracle it actually gets accepted, the old syntax will still be there. It will not be removed or deprecated. No one will hunt you down and hold a gun to your head, demanding that you upgrade existing code to use this syntax, or write new code with it. In short, the status quo is maintained.
As far as backward-compatibility, this is no different from taking advantage of any new language feature in any new release of PHP. You choose your lower-bound version number, and take advantage of any features from there up.
I'm fairly indifferent on this issue. Like Grant, I'd much rather see "->" kick the bucket long before I campaign for square bracket array definitions.
An important consideration is how this will affect the future evolution of PHP as it moves further into objects, away from its initial procedural implementation. This approach that you propose was the same taken for Ruby, another language I enjoy playing (ahem!) I mean coding in. I always liked that, and wonder what arrays would look like in future PHP versions if this approach was not taken...
I like the proposed change, and don't think it should be such a big dealio.
While we're at it, can we PLEASE, PLEASE have an unless control structure? Man, that would seriously cut down on the horrid "if (not) this is not equal to that, do nothing, else do this" nonsense littering the internet.
I'm waiting this feature for a long time. I think it's something PHP need (among other things) to get the language to the next level. When I begin developing something from the ground with PHP it feels a lot likeI'm coding on C, now C IMHO is a low level language and I think PHP can't continue being a low level lang.
Development speed is not only based on the typing speed of the programmers, you code your app one time, but how many times do you (or others) read your code? that's where, I think, this is a great feature, the code is simpler to read and to understand, so you can understand what's going on in less time. I think if the code is simpler to read we can have a lower dev time, conding faster, testing faster, modifying faster, etc.
I think everyone can see this at a glance and now whats going on:
$a = [name:"Robert", lastname:"Paulson", age:42];
(This is the way Groovy declares it's associative arrays or mappings. BTW, Groovy is a dynamic language based on Java).
Cheers,
Pablo Pazos Gutierrez
www.SimpleWebPortal.net
It is a better sintax but only that.
I love this syntax; however, there are some more important things I would like to see in PHP first, for example:
array(...)[0]
(new stdClass)->...
In fact, a real change I want to see is first-class functions and classes. Unfortunately I don't think those will ever come.
I would also like to have this syntax in PHP.
after 18 years of C++ I am entering the world of PHP/Javascript
and I absolutely love the dynamic nature of it. I have been
interested in Functional Languages and Single/Slots before
and have been employing Boost C++ library for many of
those features.
I think the array Syntax and JSON should be unified.
Furthermore, I think that
Class syntax and Array Syntax and JSON should be unified.
What I find very useful and powefull to be able to manipulate
Data as Data and then Data as Class Structures (including methods).
Assigning a function to an array element is extremely usefull
construct.
I do not think PHP is uncapable of supporting it. As a dynamic
system PHP is well suited for it, it already can assign
functions to variables.
So I would vote for Class+JSON+Array construct to be in PHP as soon
as possible.
If there is another vote coming up or a place to express my humble
opinion -- please let me know where.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.
I would actually kill for having this. I mean seriously, this is about the greatest low-hanging fruit kind of update I could see PHP implement ever!