On Mon, May 10, 2021, at 12:16 PM, Hossein Baghayi wrote:
> If constructor is supposed to be empty then why are we even bothering with
> construction promoting style? Why aren't we sticking to the old way of
> defining properties?
> Are there any advantages to using construction promoting style?
> 
> I mean sure we could have this:
> class Foo {
>     public function __construct(
>         private Bar $bar,
>         private Baz $baz
>     );
> }
> 
> but in my opinion, the old way of defining them is nice and concise:
> class Foo {
>     private Bar $bar;
>     private Baz $baz;
> }
> 
> What is the point of taking the hard way and then making it convenient?

1) Please don't top-post.

2) The reason is that the old way doesn't provide any way to populate them on 
construction.  The pattern of "assign the arg to the prop in the constructor" 
is stupendously common, and promotion makes it vastly more convenient.

For more details:
https://platform.sh/blog/2020/php-80-feature-focus-constructor-property-promotion/

The constructor isn't "supposed" to be empty.  It just often is with promotion, 
because the constructor wasn't doing anything else besides shuffling its 
parameters over to properties, which is now automatic.

--Larry Garfield

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to