FAQ
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/

Search Discussions

  • Matthew Leverton at Oct 23, 2013 at 6:03 pm
    Seems like a natural fit to me.

    Does it also work with the new foreach/list combo?

    foreach ($array as list(&$a, $b)) {}

    --
    Matthew Leverton
    On Wed, Oct 23, 2013 at 12:50 PM, Andrea Faulds wrote:
    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/

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
  • Andrea Faulds at Oct 23, 2013 at 6:07 pm

    On 23/10/2013 19:03, Matthew Leverton wrote:
    Seems like a natural fit to me.

    Does it also work with the new foreach/list combo?

    foreach ($array as list(&$a, $b)) {}
    Somehow I managed to forget to test that. Apparently, it does:

    ajf@raspberrypi ~/PHP/php-src $ sapi/cli/php -r '$arr = [[1,2],[3,4]];
    foreach ($arr as list(&$a, $b)) $a = 7; var_dump($arr);'
    array(2) {
        [0]=>
        array(2) {
          [0]=>
          int(7)
          [1]=>
          int(2)
        }
        [1]=>
        array(2) {
          [0]=>
          &int(7)
          [1]=>
          int(4)
        }
    }

    I should probably add a test for this.
    --
    Andrea Faulds
    http://ajf.me/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedOct 23, '13 at 5:50p
activeOct 23, '13 at 6:07p
posts3
users2
websitephp.net

2 users in discussion

Andrea Faulds: 2 posts Matthew Leverton: 1 post

People

Translate

site design / logo © 2022 Grokbase