Re: Constructor parent property promotion
| From: | Mathieu Rochette | Date: | Wed, 02 Sep 2020 00:00:53 +0000 |
| Subject: | Re: Constructor parent property promotion | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111772@lists.php.net to get a copy of this message | ||
On 8/17/20 7:14 PM, Lokrain wrote:
On Mon, 17 Aug 2020 at 19:46, Mathieu Rochette <mathieu@rochette.cc> wrote: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>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 listWith 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:```phpclass MyCommand extends Command{public function __construct(private Connection $connection,Why do you use visibility modifier inside here?
-- Mathieu Rochetteparent string $name,) {C# has something like this ) : base($name)}}```This would be the equivalent of :```phpclass 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:```phpclass 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