Re: [PHP-DEV] [RFC] [DISCUSSION] Compact Object Property Assignment

This is only part of a thread. view whole thread
  109083
March 17, 2020 01:13 jakob@givoni.dk (Jakob Givoni)
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
  109115
March 17, 2020 21:33 matthewmatthew@gmail.com (Matthew Brown)
I don't think it's conflicting _if you insist on brackets_ after the new
expression. PHP Parser grammar for "new" expressions is

T_NEW class_name_reference ctor_arguments

If you allowed the shorter syntax, ctor_arguments would allow an
object_properties entry.

I'm sure Nikita could clarify, though.


On Mon, 16 Mar 2020 at 21:13, Jakob Givoni <jakob@givoni.dk> wrote:

> 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 >
  109119
March 18, 2020 02:42 jakob@givoni.dk (Jakob Givoni)
On Tue, Mar 17, 2020 at 4:33 PM Matthew Brown <matthewmatthew@gmail.com> wrote:
> I don't think it's conflicting _if you insist on brackets_ after the new expression. PHP Parser grammar for "new" expressions is > > T_NEW class_name_reference ctor_arguments > > If you allowed the shorter syntax, ctor_arguments would allow an object_properties entry. Sounds good, but this is unfamiliar territory to me, so would you mind
giving an example of an alternative syntax you think would work?
> I'm sure Nikita could clarify, though. I know Nikita is busy but I hope he'll stop by this RFC one of these
days, as I have a few things that needs his expertise :-)