atom feed27 messages in net.php.lists.internalsRe: [PHP-DEV] SUMMARY: Array syntax
FromSent OnAttachments
Johannes SchlüterJan 11, 2008 7:47 am 
Steph FoxJan 11, 2008 8:51 am 
PierreJan 11, 2008 9:37 am 
PierreJan 11, 2008 9:38 am 
Stanislav MalyshevJan 11, 2008 9:49 am 
Johannes SchlüterJan 11, 2008 10:24 am 
PierreJan 11, 2008 10:27 am 
Rasmus LerdorfJan 11, 2008 10:33 am 
Sam BarrowJan 11, 2008 10:47 am 
Olivier HillJan 11, 2008 11:06 am 
Stanislav MalyshevJan 11, 2008 11:07 am 
Sam BarrowJan 11, 2008 11:09 am 
Sam BarrowJan 11, 2008 11:13 am 
Steph FoxJan 11, 2008 11:17 am 
Antony DovgalJan 11, 2008 1:52 pm 
PierreJan 11, 2008 2:08 pm 
Sam BarrowJan 11, 2008 2:13 pm 
Antony DovgalJan 11, 2008 2:35 pm 
Jeff GriffithsJan 11, 2008 3:20 pm 
Antony DovgalJan 11, 2008 3:36 pm 
Gregory BeaverJan 11, 2008 4:09 pm 
Jeff GriffithsJan 11, 2008 4:54 pm 
PierreJan 11, 2008 5:02 pm 
Marcus BoergerJan 12, 2008 3:41 am 
Tomi KaistilaJan 12, 2008 6:17 am 
Kouber SaparevJan 14, 2008 7:27 am 
Richard LynchJan 15, 2008 2:12 pm 
Subject:Re: [PHP-DEV] SUMMARY: Array syntax
From:Gregory Beaver (
Date:Jan 11, 2008 4:09:15 pm

Jeff Griffiths wrote:

Antony Dovgal wrote: ...

Well, to me it matters whether the author is going to care of the thing he's proposing or he's going to disappear right after it's implemented.

I didn't realize there was a section of the code flagged 'syntactic sugar' and only a few people maintained that part. it also seems ridiculous to debate a syntax *addition* such as this in terms of maintainability. Is it really is hard to maintain this patch to the parser? How hard is it to maintain this:

$array[] = $foo;

...vs any other part of the codebase?

Whoa, slow down cowboy!

Any change to the parser can have unexpected and even bizarre ramifications in code. For instance,

$a = $b([0]);

would become perfectly legal syntax, and although it is pretty to those of us who pine after ASCII art, its action is not as obvious as:

$a = $b(array(0));

This is just from a user perspective.

Now, from an internals perspective, taking a look inside zend_languager_parser.y, currently, it is easy to tell when an array is desired. T_ARRAY always portends an array. With the new syntax, this is no longer as deterministic. '[' is used for array initialization as well as array access. Seeing a '[' in isolation, it is not enough information to know whether this is array creation or array access. In fact, as the example above shows, the identical syntax is valid for creation [0] and access [0]. Even with careful attention to precedence and ordering (does '[' bind to the right or the left side of the expression?) it can be very easy to introduce bizarre parse or compile errors.

Now, this may sound blunt, but the tone of your message suggests you haven't worked much with parser generators or lexer generators in C. They're one of the more quirky and finicky aspects of PHP internals, and seemingly simple changes can result in ridiculously complex problems.

I think a lot of web developers who use PHP would agree with Rasmus' sentiment[1]:

"What is clear and understandable to web developers is a moving target. As someone mentioned, nobody who does any sort of web development today can ignore Javascript and they will typically be switching back and forth between Javascript and PHP every couple of minutes. This is our target user these days and as such this syntax is appropriate I think."


Having said all of the above caveats, I personally am on the fence regarding the new syntax. I was also confused when I first started working with PHP about the use of array() for array creation, but this lasted less than an hour, and then I was grateful for the simplicity. Later I grew tired of typing "array", but I type pretty fast, so it's not really an issue.

I am used to arrays, and find them intuitive. I am a bit concerned about code like this rearing its head:

if (1) { $a = [ [ ['a' => 1, 3], ($b = 3), ] ]; } else { $a = [ [ ['b' => 1, 3] ] ]; }

"bracket hell" is not something I've associated with PHP much, and the new syntax would definitely make it possible, but would also simplify the amount of spurious stuff on the screen for me in my work if used well.

So, I am +0.5 for [] syntax in principle. I have not examined the patch.