Re: Allowing `(object)['key' => 'value']` in initializers?
| From: | Lynn | Date: | Mon, 27 Sep 2021 14:50:33 +0000 |
| Subject: | Re: Allowing `(object)['key' => 'value']` in initializers? | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-116180@lists.php.net to get a copy of this message | ||
On Mon, Sep 27, 2021 at 3:15 PM Sara Golemon <pollita@php.net> wrote:
> I think for consistency's sake, we'd want to provide a base constructor to
> stdClass to take an associative array that maps into dynamic properties,
> e.g.
>
> class stdClass {
> public function __construct(array $props) {
> foreach ($prop as $name => $val) {
> $this->$name = $val;
> }
> }
> }
>
> Then you could:
> php> static $x3 = new stdClass(['key' => 'value']);
>
If the structure of
$something = {}; would ever be allowed in PHP, the
following could be done instead: $something = { ...$array };. This will
reduce the dependency on the stdClass.