Press "Enter" to skip to content

PHP RFC: Letting PHP's foreach Support list()

Last month, I finally joined the PHP developer team. The biggest hurdle all along has been the language; looking back now, I really should have worked harder at learning English back then.
My first task was: to resolve a feature request — allow foreach($array as list($a,$b).
The gist is that PHP should support the following syntax:

<?php
foreach (array(array(24,2333), array(31,4666)) as $k => list($a, $b)) {
    printf("key:%s, a=>%s, b=>%s\n", $k, $a, $b);
}
/** output:
key:0, a=>24, b=>2333
key:1, a=>31, b=>4666
*/

The starting point for implementing this feature is to rewrite PHP's syntax-parsing logic. The specific implementation — I'll attach the patch for PHP 5.4 later. Those interested can also refer to one of my earlier articles, Customizing Your Own PHP Syntax — Implementing unless in PHP.
However, in the later vote on new features to include in PHP 5.4, the current tally is 9 in favor, 19 against. It looks like 5.4 will not include this feature.
All I can hope for is that it gets added in the next major version upgrade.
I'll release the RFC and the Patch first, so that those interested in this part of PHP's syntax parsing can refer to them.
RFC: https://wiki.php.net/rfc/foreachlist
Patch for PHP 5.4: http://www.laruence.com/php-dev/php-5.4.0svn-add-foreach-list-with-slient-support.patch
Also, it's worth mentioning that starting from PHP 5.4, the changes in the syntax-parsing area are very large — the basic data structures have changed a lot. For those who want to study the PHP language engine, this is something to be aware of.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.