Hi Olumide, czw., 12 wrz 2019 o 17:07 Olumide Samson <oludons...@gmail.com> napisał(a):
> The RFC is a beautiful feature suggestion, but something is telling me as > beautiful and straightforward the syntax is, what can the use case be? > > I really love Constructor or Object initialization to be implemented in > PHP, but is there something I think you're missing in the RFC? > > Thanks for the RFC. > I'm open to suggestions. PHP is known for borrowing features from other languages and this feature is also known from other languages. * Rust allows whole struct initialization in one expression, although these are not classes but being able to instantiate, initialize and return instance directly after it's being initialized reduces a lot of boilerplate. * C# uses an object initializer for the same purposes, one expression which allows to instantiate, initialize and use the object directly after initialization. * Java users use hacks to do the same, even more, cause they're allowed to call logic through method calls inside "initializer" block. In some cases when you deal a lot with DTO objects, object instantiation and properties initialization is very verbose and introduces a lot of noise. class CustomerDTO { public Uuid $id; public string $name; public Email $email; public Address $address; public City $city; public Country $country; public PhoneNumber $phoneNumber; public etc... } Given above DTO class instantiation and initialization now requires: * INSTANTIATE class and assign to a variable: $obj = new CustomerDTO(); * FOR EACH (can be many) needed property use the variable with object instance and assign the property value: $obj->property = $value; Now when you deal a lot with objects like that you can see you're constantly repeating yourself with $obj-> part all the lines down before you probably would use that instance. There is no need for the constructor in such objects, they're purpose is to transfer data between app layers. Last months I've been working on a project which often requires to create an entity with data decoded from JSON format, most of them had many fields|properties required and only some of them were optional (even those optional were nullable so possibly I could initialize them with null once again), so the case with which I had a lot to do was creating factories which instantiate and for each property initialize value just to be able to return newly created instance in the end. That's when I thought object initializer could reduce a lot of boilerplate. That's when I started thinking of many places where I could use that, where my IDE could help me to write code faster without all that noise of constantly repeating myself. The syntax is similar to other languages. The narrow case described in BC changes is already deprecated and in my opinion, probably even not used or very rare. The future scope features can potentially save even more strikes but that's not the main reason about that RFC - nice to have but let's start with something. Regards, Michał Brzuchalski