Re: Optional constructor body

From: Date: Thu, 18 Jul 2024 23:37:59 +0000
Subject: Re: Optional constructor body
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Fri, Jul 19, 2024 at 1:34 AM Oliver Nybroe <[email protected]>
wrote:

> Hello internals.
>
> I am looking into making the constructor body optional in classes,
> essentially allowing you to write
>
> ```
> class User {
>     public function __construct(
>         private string $name,
>     )
> }
> ```
>
> Currently to make this code valid, it would have to be written the
> following way
>
> ```
> class User {
>     public function __construct(
>         public string $name,
>     ) {}
> }
> ```
>
> With the introduction or constructor property promotion in 8.0, we often
> see classes where the constructor has an empty body, and my guess would be
> that this will only increase with the introduction of property access hooks
> in 8.4 which is allowed to be defined in the constructor also.
>
> This change would only be a cosmetic change and simplify the userland code
> by removing two redundant characters.
>
>
>
> This would be my first RFC and I am willing to try and implement it
> myself.
>
>
> Best regards
> Oliver Nybroe (he/him)
>


FWIW, there is already a PR for it (
https://github.com/php/php-src/issues/8420)
which also contains the RFC
draft.

Kind regards,
Faizan


Thread (15 messages)

« previous php.internals (#124504) next »