# [RFC] Allow specifying keys in list()

- Thread: https://externals.io/message/90699
- Original archive: https://news-web.php.net/php.internals/90699
- Started by Thomas Bley on 2016-01-18T13:19:11+00:00
- Messages: 4
- RFC: Allow specifying keys in list() (Implemented (PHP 7.1)) — https://externals.io/rfc/list_keys.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #90699 — Thomas Bley — 2016-01-18T13:19:11+00:00

URL: https://externals.io/message/90699#90699

What about using objects with list()?

class Point {
public function __construct($x, $y) {
  $this->x = $x;
  $this->y = $y;
}
}

$point = new Point(21, 42);
list('x' => $x, 'y' => $y) = $point;

Regards
Thomas

Andrea Faulds wrote on 17.01.2016 03:32:

> Hi everyone,
> 
> Here's an RFC that would extend the syntax of list() to be more useful 
> with associative arrays:
> 
> https://wiki.php.net/rfc/list_keys
> 
> Please read it and tell me your thoughts.
> 
> Thanks!
> -- 
> Andrea Faulds
> https://ajf.me/

---

## #90706 — Andrea Faulds — 2016-01-19T01:35:10+00:00

URL: https://externals.io/message/90699#90706 · In reply to: #90699

Hi Thomas,

Thomas Bley wrote:
> What about using objects with list()?
>
> class Point {
> public function __construct($x, $y) {
>    $this->x = $x;
>    $this->y = $y;
> }
> }
>
> $point = new Point(21, 42);
> list('x' => $x, 'y' => $y) = $point;

You can't do `$x = $point['x'];`, so `list("x" => $x) = $point;` won't 
work either. If you want list() support, implement ArrayAccess.

We could potentially add some extension to list() for object 
destructuring, but I'm not sure I'd be happy with that, as list() has 
thus far been an array-only thing. If we ever add object literals, then 
we could do object destructuring with a symmetrical syntax.

Thanks.
-- 
Andrea Faulds
https://ajf.me/

---

## #90712 — Lester Caine — 2016-01-19T11:49:41+00:00

URL: https://externals.io/message/90699#90712 · In reply to: #90706

> > […]
> 
> You can't do `$x = $point['x'];`, so `list("x" => $x) = $point;` won't
> work either. If you want list() support, implement ArrayAccess.
> 
> We could potentially add some extension to list() for object
> destructing, but I'm not sure I'd be happy with that, as list() has
> thus far been an array-only thing. If we ever add object literals, then
> we could do object destructuring with a symmetrical syntax.

Andrea, please bare with me as I'm trying to work out just what some of
those long words actually mean.

My real world example is from probably 15+ year old code which has to be
re-factored these days to actually work. It creates an 'object' or list
of objects as a multi level array to pass around and then uses `extract()`
to give a set of variables some of which may be arrays themselves ....
and some 'NULL' entries ... depending on just what the template is
building. The 'parsing' will select a function depending on the type of
a resulting element and perhaps pass on the extracted array if that is
what is provided.

I understand that modern practice is to actually define all the values
being used ( and type them ) but the flexibility that used to be
available seems to be lost by some of these new approaches when one now
expects an array of 'objects' rather than an array of 'elements' ? I
think that your 'destructuring' is the difference between my simply
using the array value and having to find the value of a private variable
in the object?

Since I am invariably working with associated arrays, 'list' is not
something in my toolkit, which is why using it will require rethinking,
but simply tidying 'extract' to create a matching set of variables
inside the object would make sense in my method of working. Every
persistent object comes into me as an associated array, so I'm trying to
see how this might improve my own code since the current practices are
no longer PC.

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #90716 — Rowan Collins — 2016-01-19T16:18:52+00:00

URL: https://externals.io/message/90699#90716 · In reply to: #90712

Lester Caine wrote on 19/01/2016 11:49:
> > […]
> Andrea, please bare with me as I'm trying to work out just what some of
> those long words actually mean.

I think what Andrea's getting at is being able to do something similar 
to what you can do in JS:

// Object literal: create an object with all its values
var foo = { a: 1, b: 2, c: 3 };
// Destructure an object into variables: take bits of an object that 
you're interested in all in one go
// equivalent to bob = foo.a; jane = foo.c;
var {a: bob, c: jane} = foo;

In PHP, we don't have either of these on objects - you can only create 
an object using a constructor function - but the proposal is basically 
the same thing for associative arrays (which are in some ways similar to 
JS objects anyway):
// Array literal
$foo = ['a' =>  1, 'b' => 2, 'c' => 3];
// Destructuring with list()
list('a' => $bob, 'c' => $jane);

So it's like `extract()`, but both more powerful and less dangerous - you 
don't get an unexpected variable $b when someone adds a key to the 
array, and you have the ability to rename $a and $c while you're 
extracting them.

It may or may not work for your use case, but it doesn't really have 
anything to do with objects either way.

Regards,
-- 
Rowan Collins
[IMSoP]
