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

From: Date: Fri, 12 Feb 2016 14:55:39 +0000
Subject: Re: [RFC][VOTE] Allow specifying keys in list()
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-91222@lists.php.net to get a copy of this message
S.A.N wrote on 12/02/2016 14:39:
2016-02-12 16:27 GMT+02:00 Rowan Collins <rowan.collins@gmail.com>:
S.A.N wrote on 12/02/2016 13:37:
Often all keys are unknown, or a very lot keys, use list(...) - is unreal. I would like to, instead <?php foreach($params as $key => $value) { $this{$key} = $value } Use the short syntax sugar <?php $this += $params ?> It's really do?
If the keys are unknown, then you probably don't want to blindly copy them onto object properties; at that point, you might as well just have $this->data and leave them as an array. It sounds like what you actually want is an object literal syntax - i.e. $params should never have been an array in the first place.
I would operator (+=) as like to function Object.assign() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
Sure, but I was thinking more in terms of whether this is a strong use case for having it. In JS, "Object" is the appropriate type for an arbitrary hash of key-value pairs. In PHP, the appropriate type for such a structure is "array", which already supports the + operator and array_merge function. Note that the method you linked doesn't copy from an array onto an object, it copies from one object to another. Doing the same in PHP leaves the question of how you create the right-hand object, which is why I mentioned "object literal syntax", i.e. the ability to write something like "{ a => 42, b => 'Hello' }" or "new Foo { a => 42, b => 'Hello' }" to define an object with directly-specified property values, rather than running the constructor. The constructor example using list() syntax is deliberately naming the fields we're interested in, because it's populating an object of a particular class, with known property names, not dynamically creating arbitrary property keys. Regards, -- Rowan Collins [IMSoP]

« previous php.internals (#91222) next »