Re: [RFC] Constructor Property Promotion
| From: | Nikita Popov | Date: | Wed, 08 Apr 2020 14:18:28 +0000 |
| Subject: | Re: [RFC] Constructor Property Promotion | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-109571@lists.php.net to get a copy of this message | ||
On Wed, Apr 8, 2020 at 1:56 PM Nicolas Grekas <nicolas.grekas+php@gmail.com>
wrote:
>
> I would like to submit the following RFC for your consideration:
>> https://wiki.php.net/rfc/constructor_promotion
>>
>
> I love it :)
>
> Just one question:
> Shouldn't default values be copied on the signature? That would be
> expected for me.
> (and that would make the properties still initialized when child classes
> don't call the parent constructor for whatever reasons.)
>
> class Point {
> public function __construct(
> public float $x = 0.0,
>
> <==>
>
> class Point {
> public float $x = 0.0;
>
> public function __construct(
> float $x = 0.0,
>
> Nicolas
>
Some reasons why the default value is not duplicated into the property are
discussed in https://wiki.php.net/rfc/constructor_promotion#desugaring.
It's primarily about forward-compatibility concerns.
But apart from that, I strongly believe that you should not, ever, specify
a default value on a property that is initialized in the constructor. You
can have a default, or you can have explicit initialization in the
constructor -- both together do not make sense.
Regards,
Nikita