Re: [RFC] Allow specifying keys in list()

From: Date: Tue, 19 Jan 2016 16:18:52 +0000
Subject: Re: [RFC] Allow specifying keys in list()
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-90716@lists.php.net to get a copy of this message
Lester Caine wrote on 19/01/2016 11:49:
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.
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]

« previous php.internals (#90716) next »