Re: [PHP-DEV] [RFC] Object Initializer

This is only part of a thread. view whole thread
September 13, 2019 09:19 (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=)
Hi Arnold,

pt., 13 wrz 2019 o 10:51 Arnold Daniels>

> This can easily be done in a number of ways, like the suggested helper > function. The same can be said for many other features that were > implemented recently, like array unpacking. This feature is easy to > implement and will make the code for data objects much more readable, with > additional benefits for static analysis. > > Comments: > > I don't like how it works for anonymous classes. It's more difficult to > implement since the compiler doesn't know the meaning of the (first) > bracket. It's doesn't make the code more readable, for the same reason. I > think it's better to not support this syntax with anonymous classes. > > Probably lexical scope for anon classes would be better here, but due to
fact that proposal is to use initializer block instead of constructor arguments, that was the reason why initializer block got before anon class definitions.
> The examples do not show how constructor arguments are passed. I'm assuming > it's > > $customer = new Customer("foo") { > name = "John" > }; > > The examples don't show that cause it's forbidden.
There is a note on that in RFC on purpose
> Note! Object instantiation allows only constructors without required arguments to be used.
> Any class which requires passing arguments to constructor cannot be used in combination with object initializer.
Using constructor arguments and object initializer would introduce noise and you'll be potentially initializing object twice: using object initializer block and using constructor args what may be misleading.
> About the idea of letting `{ foo = 10 }` create an `stdClass` object (not > in the RFC); While not used much since it has no effect, it's perfectly > okay to put your code in brackets eg `{ { { $foo = 10; } } }`. As such, I > don't think it's a good idea to allow `new stdClass` to be omitted. >
Future scope mentions only about letting to create stdClass with omitting of the class name only, nothing said about removing a new keyword. Thanks, Michał Brzuchalski