Proposal: Implement FR #7930 (list() constructor reference assignment)
| From: | Andrea Faulds | Date: | Wed, 23 Oct 2013 17:50:24 +0000 |
| Subject: | Proposal: Implement FR #7930 (list() constructor reference assignment) | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-69809@lists.php.net to get a copy of this message | ||
Good evening everyone,
I have a simple suggestion for a feature addition, which I have already implemented. Over 13 years ago, someone suggested adding support for assigning by reference in list(), with the following syntax:
$arr = [1, 2]
list(&$a, $b) = $arr;
Which would be equivalent to:
$a = &$arr[0];
$b = $arr[2];
Their request is here: https://bugs.php.net/bug.php?id=6768
It seemed like a useful feature, so I implemented it. The syntax is a little unusual, in that normally you do $a = &$b, and with list() you'd do list(&$a) = [$b], but I don't think there's a better syntax for it that allows you to set some variables as references, but not all.
The patch is here: https://github.com/TazeTSchnitzel/php-src/compare/ListByReference
If people think it is useful, I'll write an RFC soon.
Thanks,
--
Andrea Faulds
http://ajf.me/