24 comments
ken Says:
I've wanted this for awhile, too. It just makes sense IMO. I get painful flashbacks to VB when I have to write out "array(...)", but I guess I can be thankful that it doesn't have to be Array(...)!
@brian
Thanks for the article, this is the first I've heard of this proposal I believe. You have great arguments, except for: "It is a language construct and should look like a language construct." The reason I say this is that there are other far more insidious language constructs in PHP (for example: echo, include, and empty to name a few), and so saying that it should look like the rest isn't exactly accurate. It looks like maybe you meant 'primitive data type'? Either way, sorry to harp on a detail, I still knew what you meant from your examples :)
@Jared
Anonymous object, or anonymous function? Your example seems to try to combine both. I thought you were at first referring to the way JavaScript does such things, but that syntax would be invalid. Also, I don't think we're likely to see either anytime soon in PHP, unless PHP undergoes radical changes. Being able to pass around functions as parameters as you demonstrated is a feature of languages in which functions are first-class objects (which I think would be a prototype language), and functions do not have this status in PHP. PHP does have create_function(), yet it is clumsy and has other problems (such as never being released to the GC).
An 'anonymous' object (stdClass ??) has no value at all in userland that I have ever found; you can't add methods to it, you can't control member visibility or access, and basically all you have is an array with a different syntax -- which lacks access to the tons of built-in array functions.
@Mikael
+1 for $fn()[n] !
Tanel Says:
Why not split list and hash implementations with {'foo' => 1, 'bar' => 2} and [1, 2, 'foo'] respectively?
Marc Gear Says:
I'm right behind this addition to the language, and like Brian, hope it just gets committed :)
Kae Verens Says:
agreed - great idea. can't tell you how many times I've skipped from writing some javascript to writing some php and starting off an array with [], then slapping my head when the server complains.
Raven Says:
I like it. Those little details make other languages so pretty and handy. (Imho for a language that claims to focus on pragmatics PHP's syntax is sooo inflexibile atm. Like the previous speakers I'd very much like to have anonymous functions and the $myVar = explode($foo,’;')[1]; notation in the future, as well as a Perl-like regex operator and other stuff that reduces verbosity.) Thanks for your effort on this nice little addition!
techfounder » PHP syntax mixings Says:
[...] more readable and more welcoming to programmers coming from other languages. I read about this on Brian Moon’s blog, and among one of the comments I read an interesting thought: Yes, would be a nice addition. Though [...]
harald Says:
i too think, that a short object notation as replacement for 'new stdClass' would be very, very helpful and i would appreciate the [...] for arrays -- it would be very useful to cleanup code and make it more readable. but i bet neither of them will come -- if it's not because it's to hard to implement, it will be because of political reasons -- imo.
Short Array Syntax for PHP | PHP Readings Says:
[...] Brian Moon posted new article how you can define arrays in shorter way. He also has wrote his opinion why we should use this [...]
Tomek Says:
I say no - I personally hate things like that, when you can do one thing in gazzilion of ways, see JavaScript as the best example of how bad it can become - I am all for readability of code and this is totally against it.
Jared Says:
Gazillon ways?
Try 2. [] and new Array().
Short Array Syntax for PHP | Mats Lindh Says:
[...] route from the aggregated stream of Planet PHP comes a small post from Brian Moon about Stan’s suggestion for introducing the [] syntax for creating lists in [...]
Michał Mech Says:
They should focus on important things not on such unnecessary details like this additional syntax.
PHP has enough aliases and mess without any consequence in naming convection, etc...
Jared Says:
Yes, would be a nice addition. Though I'd rather the anonymous object syntax came too, would be much more useful, with callbacks etc.
visit({ function accept() { ... } });
Mikael Says:
I like that.
That and $myVar = explode($foo,';')[1];
sofia Says:
i vote it up too :)
it would be nice to be able to do getSomething(['key'=>'value']); instead of getSomething(array('key'=>'value'));
Just looks prettier and more concise :)
Steve Francia Says:
Been using PHP since early 3. I have always wondered why this hasn't been a part of it. I have always thought it is a must have.
Stefan Says:
Even though I think the short notation is nice, I still dislike having even *more* ways to do the same thing in PHP. If we add this short notation, why not get rid of the array() alltogether?
MonkeyT Says:
I'm all for it. I do some teaching of PHP to beginners, so I understand about the comprehension problems with newbies, and the confusion of the syntax ("square brackets denote keys, not values, don't they?"), but I think the benefits far outweigh the drawbacks.
Matthew Purdon Says:
I think this addition is so minor and so common in other languages that it should have been a direct commit. What were some of the reasons for the people who voted against it?
Comments are disabled for this post.



Johny Says:
Well i think addition is useless.
If i decide to use it and maybe customer has lower PHP version it will cause Problems so i have to use old codes anyway.
Disadvantag 2, if Developers who do not read all PHP news anyday see mit code they might not know what it is.