On Tue, May 11, 2021 at 4:37 PM Côme Chilliet <
come.chill...@fusiondirectory.org> wrote:

> Le Tue, 11 May 2021 10:58:57 +0200,
> Nikita Popov <nikita....@gmail.com> a écrit :
>
> > If we allow it, I would restrict it to specifically the case of a) a
> > promoted constructor b) which has *only* promoted parameters. I don't
> think
> > we should allow replacing "{}" with ";" for methods in the general case.
>
> This would create a subtle difference of allowed syntax between a
> constructor
>  and an other method, and between a constructor with promoted parameters
> and
>  without. I think we should avoid this.
>
> I even thought we merged changes to make constructors more like the other
>  methods, but I just checked and it seems the RFCs to allow ": void" on
>  constructors were declined.
>

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.

I don't think there's any particularly good reason to have a body-less
constructor outside the promotion case. The example given by Bruce

    private final function __construct();

is going to throw a warning since PHP 8 -- while I lobbied against that
decision at the time, final private methods no longer have an effect, and
declaring such a constructor will buy you nothing apart from that warning.

If a body-less non-promoted constructor is used, the most likely assumption
would be that the programmer forgot an "abstract" modifier.

Is there some use-case for empty constructors that I'm missing here?

Regards,
Nikita

Reply via email to