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

This is only part of a thread. view whole thread
  109122
March 18, 2020 05:27 michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=)
śr., 18 mar 2020, 03:36 użytkownik Jakob Givoni <jakob@givoni.dk> napisał:

> Thank you, Michał, for chiming in :-) > > On Tue, Mar 17, 2020 at 10:52 AM Michał Brzuchalski > brzuchalski@gmail.com> wrote: > > For object initializer, I was hoping to introduce a feature which with > the benefits of typed properties > > could reduce the boilerplate on initializing object and setting their > properties in one expression. > Exactly my motivation with COPA as well; > - Typed properties - check, > - Reduce boilerplate - check, > - Initializing and setting properties in one expression - check. > > > With object initializer, this could be reduced to: > > > > $this->dispatch(new SomeCommand { foo = 'bar', baz = false }); > And with COPA it would be; > $this->dispatch((new SomeCommand)->[ foo = 'bar', baz = false ]); > Subtle differences, when compared to the currently available alternative. >
The difference is in object state initialization : Using Object Initializer enforce that if a class is instantiated with the Object Initializer, at the end of the instantiation and properties initialization, all visible properties *(depends on initialization scope)* are initialized, otherwise, a RuntimeException is thrown. This helps to avoid bugs where a property is added to the class but forgot to be assigned it a value in all cases where the class is instantiated and initialized. https://wiki.php.net/rfc/object-initializer#restrictions You can't do that with COPA cause it has nothing to do with object instantiation.
> > But as mentioned IMO this is a different feature than what you propose > and personally I see no point > > in reducing only assignment statements without combining it with object > construction for these kinds > > of small objects, DTO's, commands, queries and events. > Maybe you missed that COPA can be combined with object construction? > > > but personally I don't need a constructor at all in those cases since we > > have typed properties and it looks like they could be also marked as > read-only in next major PHP version. > Exactly, and as I couldn't help notice that you voted for the Write > Once Properties RFC, which clearly states that > object construction is a fuzzy term (meaning don't take it too > seriously) and lazy initialization is a feature... > > I hope you will either reconsider your support for COPA or let me know > what I'm missing so I can > consider other directions. >
  109175
March 20, 2020 23:47 jakob@givoni.dk (Jakob Givoni)
On Wed, Mar 18, 2020 at 12:27 AM Michał Brzuchalski
brzuchalski@gmail.com> wrote:

> Using Object Initializer enforce that if a class is instantiated with the Object Initializer, > at the end of the instantiation and properties initialization, all visible properties > (depends on initialization scope) are initialized, otherwise, a RuntimeException is thrown. > This helps to avoid bugs where a property is added to the class but forgot to be assigned > it a value in all cases where the class is instantiated and initialized. https://wiki.php.net/rfc/object-initializer#restrictions
Got it. Out of curiosity, would you be able to summarize why you believe your proposal was not accepted?