Yes, just to clarify the scope of my initial proposal, this should only ever apply to promoted constructors that have 1 or more promoted parameters, and no not-promoted parameters.

These would NOT be considered valid:

   public function __construct(
       private Baz $baz,
       Bar $bar
   );

   public function __construct();

   public function __construct(Baz $baz);

as well as anything not related to __construct.

- Matīss

On Tue, May 11, 2021 at 10:58, Nikita Popov <nikita....@gmail.com> wrote:
On Mon, May 10, 2021 at 10:29 AM Matīss Treinis <mrtrei...@gmail.com <mailto:mrtrei...@gmail.com>> wrote:
Hi everyone,

Since constructor property promotion is now implemented, and it looks
 like it could become a widely used feature, I am proposing a small,
 cosmetic change in syntax for constructors in concrete classes to do
 away with empty constructor body.

 Here's an example of how this would work:

 <?php
 namespace App;

 class Foo {
     public function __construct(
         private Bar $bar,
         private Baz $baz
     );
 }

 Some notes to this:

 - Since this is similar to already existing syntax for body-less
methods, parser should not be affected that much. I hope. I really have
 no idea.
 - Syntax would be optional - meaning, you can as well continue using
 empty body, just that in this case the body would be implied empty.

 Thoughts?
 Regards,
 - Matīss

For what it's worth, I've received the same suggestion from quite a few people. There seems to be an intuitive expectation that only adding a semicolon will work in this case. Personally, I'm okay with allowing it.

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.

Regards,
Nikita

Reply via email to