Re: [RFC][Draft] Body-less __construct
| From: | Nikita Popov | Date: | Tue, 11 May 2021 16:59:47 +0000 |
| Subject: | Re: [RFC][Draft] Body-less __construct | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-114415@lists.php.net to get a copy of this message | ||
On Tue, May 11, 2021 at 6:56 PM Matt Fonda <matthewfonda@gmail.com> wrote:
> On Tue, May 11, 2021 at 7:45 AM Nikita Popov <nikita.ppv@gmail.com> wrote:
>
>> My thought here is that a constructor with (only) promoted properties is
>> hardly a constructor at all -- it's more like a special syntax for
>> declaring properties that happens to re-use the constructor notation,
>> because that allows it generalize in certain ways. It could have been
>> implemented with some other syntax that didn't explicitly mention
>> constructors at all, such as class Point($x, $y, $z) {} or something.
>>
>
> Agreed. I've personally been a little hesitant to start using CPP because
> of this. Declare constructor parameters AND declare properties AND
> magically provide a constructor implementation that sets their values. It
> looks like a constructor, but it's actually a little different. For me,
> this proposal would really help clear that up.
>
> When I see a method with an empty body, I think "this method does have an
> implementation, and that implementation does exactly nothing." In the case
> of CPP, this isn't true. The method appears to do exactly nothing, but in
> fact it has an automatically defined implementation that sets the
> properties to the given values.
>
> When I see a semi-colon-terminated method, I think "this method has no
> implementation *here*; one will be provided from somewhere else." This is
> the case in abstract classes and interfaces--the subclass provides the
> implementation. And it's the case in CPP too--an implementation that sets
> the properties to the given values is automatically provided. For this
> reason, I don't see it as inconsistent. The semi-colon means someone else
> will provide an implementation, and CPP does just that.
>
After you put it into these terms, this reminds me a lot of
https://wiki.php.net/rfc/property_accessors#implicit_implementation.
For
accessors there's very much a difference between
public $prop { get; } // auto-generated get
and
public $prop { get {} } // explicit get that doesn't do anything
Making "allow replacing {} with ;" as a general feature would certainly
pose a problem for this choice of syntax (which is inspired by C#).
Regards,
Nikita