Re: [RFC] [DISCUSSION] Compact Object Property Assignment
| From: | Jakob Givoni | Date: | Tue, 17 Mar 2020 01:13:27 +0000 |
| Subject: | Re: [RFC] [DISCUSSION] Compact Object Property Assignment | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-109083@lists.php.net to get a copy of this message | ||
On Mon, Mar 16, 2020 at 9:10 AM Matthew Brown <matthewmatthew@gmail.com> wrote:
> I love the idea!
Thanks Matthew!
> The syntax for new objects looks a little verbose – would it be possible to use
>
> $foo = new Foo()[
> property1 = "hello",
> property2 = 5,
> ];
The short answer: I don't think so.
This is not possible (though it's arguably 'nicer'):
new Foo()->doSomething(); // syntax error, unexpected '->'
Not sure why, but it's necessary to wrap the instantiation in brackets:
(new Foo())->doSomething(); // Ok
If someone can explain why, it would be enlightening!
Furthermore, dropping the arrow would create conflicting syntax with
array access:
$foo[1]; // Array access, give me the element with index 1, can be
used on objects as well
$foo[a = 1]; // Property assignment... ???
I'll update the RFC to reflect your proposal.
Thank you,
Jakob