Constructor parent property promotion

  111590
August 17, 2020 16:45 mathieu@rochette.cc (Mathieu Rochette)
Hi,

 

I’m writing a message here to receive feedback on a two ideas related to
class constructors before going more in details with an RFC. I hope this is
appropriate behavior for this list

 

With the Constructor Property Promotion accepted in PHP 8, there’s a lot
less of boilerplate when writing class constructors. I’m suggesting two
additional ways of making thing even easier and I hope, readable.

 

First: I’d like to be able to automatically transfer a constructor
property to its parent constructor. I’m thinking of something like that:

 

```php

class MyCommand extends Command

{

   public function __construct(

       private Connection $connection,

       parent string $name,

   ) {

   }

}

```

 

This would be the equivalent of :

 

```php

class MyCommand extends Command

{

   public function __construct(

       private Connection $connection,

       string $name,

   ) {

       parent::__construct($name);

   }

}

```

 

The second idea is to make the constructor body optional, the first example
would now be:

 

```php

class MyCommand extends Command

{

   public function __construct(

       private Connection $connection,

       parent string $name,

   )

}

```

 

This would call the parent constructor automatically even if no "parent"
parameter are present. I could even see this enforced by linter to avoid
having logic in the constructors.

 

If there is interest I’d like to try writing an rfc and implement it. I
have not much knowledge of the php-src code but I hope this is a small
enough change that I’ll be able to tackle. So, at that time if someone is
interested in mentoring this little project I’d appreciate it :)

 

 

regards,

 

Mathieu
  111591
August 17, 2020 17:14 lokrain@gmail.com (Lokrain)
On Mon, 17 Aug 2020 at 19:46, Mathieu Rochette <mathieu@rochette.cc> wrote:

> Hi, > > > > I’m writing a message here to receive feedback on a two ideas related to > class constructors before going more in details with an RFC. I hope this is > appropriate behavior for this list > > > > With the Constructor Property Promotion accepted in PHP 8, there’s a lot > less of boilerplate when writing class constructors. I’m suggesting two > additional ways of making thing even easier and I hope, readable. > > > > First: I’d like to be able to automatically transfer a constructor > property to its parent constructor. I’m thinking of something like that: > > > > ```php > > class MyCommand extends Command > > { > > public function __construct( > > private Connection $connection,
Why do you use visibility modifier inside here?
> > > parent string $name, > > ) {
C# has something like this ) : base($name)
> > > } > > } > > ``` > > > > This would be the equivalent of : > > > > ```php > > class MyCommand extends Command > > { > > public function __construct( > > private Connection $connection, > > string $name, > > ) { > > parent::__construct($name); > > } > > } > > ``` > > > > The second idea is to make the constructor body optional, the first example > would now be: > > > > ```php > > class MyCommand extends Command > > { > > public function __construct( > > private Connection $connection, > > parent string $name, > > ) > > } > > ``` > > > > This would call the parent constructor automatically even if no "parent" > parameter are present. I could even see this enforced by linter to avoid > having logic in the constructors. > > > > If there is interest I’d like to try writing an rfc and implement it. I > have not much knowledge of the php-src code but I hope this is a small > enough change that I’ll be able to tackle. So, at that time if someone is > interested in mentoring this little project I’d appreciate it :) > > > > > > regards, > > > > Mathieu
  111602
August 17, 2020 22:11 mathieu@rochette.cc (Mathieu Rochette)
 

 

"Lokrain" lokrain@gmail.com – August 17, 2020 7:14 PM
 

>   > > On Mon, 17 Aug 2020 at 19:46, Mathieu Rochette <mathieu@rochette.cc> > wrote: >   > >> Hi, >> >>   >> >> I’m writing a message here to receive feedback on a two ideas related to
>> class constructors before going more in details with an RFC. I hope >> this is >> appropriate behavior for this list >> >>   >> >> With the Constructor Property Promotion accepted in PHP 8, there’s a lot
>> less of boilerplate when writing class constructors. I’m suggesting two
>> additional ways of making thing even easier and I hope, readable. >> >>   >> >> First: I’d like to be able to automatically transfer a constructor >> property to its parent constructor. I’m thinking of something like that:
>> >>   >> >> ```php >> >> class MyCommand extends Command >> >> { >> >>    public function __construct( >> >>        private Connection $connection, > > Why do you use visibility modifier inside here?
This the new PHP 8 feature brought by the Constructor Property Promotion RFC: https://wiki.php.net/rfc/constructor_promotion
>>        parent string $name, >> >>    ) { > > C# has something like this ) : base($name) > >>    } >> >> } >> >> ``` >> >>   >> >> This would be the equivalent of : >> >>   >> >> ```php >> >> class MyCommand extends Command >> >> { >> >>    public function __construct( >> >>        private Connection $connection, >> >>        string $name, >> >>    ) { >> >>        parent::__construct($name); >> >>    } >> >> } >> >> ``` >> >>   >> >> The second idea is to make the constructor body optional, the first >> example >> would now be: >> >>   >> >> ```php >> >> class MyCommand extends Command >> >> { >> >>    public function __construct( >> >>        private Connection $connection, >> >>        parent string $name, >> >>    ) >> >> } >> >> ``` >> >>   >> >> This would call the parent constructor automatically even if no "parent" >> parameter are present. I could even see this enforced by linter to avoid >> having logic in the constructors. >> >>   >> >> If there is interest I’d like to try writing an rfc and implement it. I
>> have not much knowledge of the php-src code but I hope this is a small >> enough change that I’ll be able to tackle. So, at that time if someone is
>> interested in mentoring this little project I’d appreciate it :) >> >>   >> >>   >> >> regards, >> >>   >> >> Mathieu > >  
-- Mathieu Rochette
  111772
September 2, 2020 00:00 mathieu@rochette.cc (Mathieu Rochette)
On 8/17/20 7:14 PM, Lokrain wrote:
> > > On Mon, 17 Aug 2020 at 19:46, Mathieu Rochette <mathieu@rochette.cc> > wrote: > > Hi, > > > > I’m writing a message here to receive feedback on a two ideas > related to > class constructors before going more in details with an RFC. I > hope this is > appropriate behavior for this list > > > > With the Constructor Property Promotion accepted in PHP 8, there’s > a lot > less of boilerplate when writing class constructors. I’m > suggesting two > additional ways of making thing even easier and I hope, readable. > > > > First: I’d like to be able to automatically transfer a constructor > property to its parent constructor. I’m thinking of something like > that: > > > > ```php > > class MyCommand extends Command > > { > >    public function __construct( > >        private Connection $connection, > > Why do you use visibility modifier inside here?
This the new PHP 8 feature brought by the Constructor Property Promotion RFC: https://wiki.php.net/rfc/constructor_promotion <https://wiki.php.net/rfc/constructor_promotion>
> > >        parent string $name, > >    ) { > > C# has something like this ) : base($name) > > > >    } > > } > > ``` > > > > This would be the equivalent of : > > > > ```php > > class MyCommand extends Command > > { > >    public function __construct( > >        private Connection $connection, > >        string $name, > >    ) { > >        parent::__construct($name); > >    } > > } > > ``` > > > > The second idea is to make the constructor body optional, the > first example > would now be: > > > > ```php > > class MyCommand extends Command > > { > >    public function __construct( > >        private Connection $connection, > >        parent string $name, > >    ) > > } > > ``` > > > > This would call the parent constructor automatically even if no > "parent" > parameter are present. I could even see this enforced by linter to > avoid > having logic in the constructors. > > > > If there is interest I’d like to try writing an rfc and implement > it. I > have not much knowledge of the php-src code but I hope this is a small > enough change that I’ll be able to tackle. So, at that time if > someone is > interested in mentoring this little project I’d appreciate it :) > > > > > > regards, > > > > Mathieu >
-- Mathieu Rochette
  111592
August 17, 2020 17:19 andreas@dqxtech.net (Andreas Hennings)
Interesting stuff!
https://wiki.php.net/rfc/constructor_promotion
I see this for the first time, but I like it :)

Now to your proposal.
Just to make sure I understand:
Would this map the parameters by name or by index?
-> I assume by index, it makes more sense.
Does it map to parent constructor parameters, or to parent properties?
-> I assume to parent constructor parameters, because parent properties
could be private.
Can the 'parent' parameters be intermixed with other parameters?
-> I assume yes, not allowing it would be an unnecessary limitation.

So if you use the 'parent' syntax, you need exactly as many parameters with
'parent' as required by the parent constructor.

// Parent class
class B {
  public function __construct($b0, $b1) {
    // The values may or may not be written to properties, it does not
matter.
  }
}

// Child class
class C extends B {
  public function __construct(parent $x, private $c0, parent $y, private
$c1) {
    // Implicit: parent::__construct($x, $y);
  }
}




On Mon, 17 Aug 2020 at 18:46, Mathieu Rochette <mathieu@rochette.cc> wrote:

> Hi, > > > > I’m writing a message here to receive feedback on a two ideas related to > class constructors before going more in details with an RFC. I hope this is > appropriate behavior for this list > > > > With the Constructor Property Promotion accepted in PHP 8, there’s a lot > less of boilerplate when writing class constructors. I’m suggesting two > additional ways of making thing even easier and I hope, readable. > > > > First: I’d like to be able to automatically transfer a constructor > property to its parent constructor. I’m thinking of something like that: > > > > ```php > > class MyCommand extends Command > > { > > public function __construct( > > private Connection $connection, > > parent string $name, > > ) { > > } > > } > > ``` > > > > This would be the equivalent of : > > > > ```php > > class MyCommand extends Command > > { > > public function __construct( > > private Connection $connection, > > string $name, > > ) { > > parent::__construct($name); > > } > > } > > ``` > > > > The second idea is to make the constructor body optional, the first example > would now be: > > > > ```php > > class MyCommand extends Command > > { > > public function __construct( > > private Connection $connection, > > parent string $name, > > ) > > } > > ``` > > > > This would call the parent constructor automatically even if no "parent" > parameter are present. I could even see this enforced by linter to avoid > having logic in the constructors. > > > > If there is interest I’d like to try writing an rfc and implement it. I > have not much knowledge of the php-src code but I hope this is a small > enough change that I’ll be able to tackle. So, at that time if someone is > interested in mentoring this little project I’d appreciate it :) > > > > > > regards, > > > > Mathieu