Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111772 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 71543 invoked from network); 2 Sep 2020 00:56:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Sep 2020 00:56:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8C57718050A for ; Tue, 1 Sep 2020 17:01:12 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=BAYES_20,HTML_MESSAGE, SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from texthtml.net (texthtml.net [62.210.148.97]) by php-smtp4.php.net (Postfix) with ESMTP for ; Tue, 1 Sep 2020 17:01:11 -0700 (PDT) Received: by texthtml.net (Postfix, from userid 65534) id 833353E28C5; Wed, 2 Sep 2020 02:01:10 +0200 (CEST) Received: from [192.168.0.112] (stunnel_mail_1.mail_default [172.22.0.5]) (Authenticated sender: mathieu@texthtml.net) by texthtml.net (Postfix) with ESMTPA id 991CE3E0053; Wed, 2 Sep 2020 02:00:54 +0200 (CEST) To: Lokrain Cc: "PHP internals (internals@lists.php.net)" References: <20200817164539.Horde.kYUsO4nuFe1V_fbKiAC6n5E@cloud.rochette.cc> Message-ID: <57788e2c-902d-86c5-65a1-d980adfe72df@rochette.cc> Date: Wed, 2 Sep 2020 02:00:53 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------8A456DADE9755C5683E3E455" Content-Language: en-US Subject: Re: [PHP-DEV] Constructor parent property promotion From: mathieu@rochette.cc (Mathieu Rochette) --------------8A456DADE9755C5683E3E455 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 8/17/20 7:14 PM, Lokrain wrote: > > > On Mon, 17 Aug 2020 at 19:46, Mathieu Rochette > 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 --------------8A456DADE9755C5683E3E455--