Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111592 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 2121 invoked from network); 17 Aug 2020 18:18:38 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Aug 2020 18:18:38 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 086DF180531 for ; Mon, 17 Aug 2020 10:19:33 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-vs1-f47.google.com (mail-vs1-f47.google.com [209.85.217.47]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 17 Aug 2020 10:19:32 -0700 (PDT) Received: by mail-vs1-f47.google.com with SMTP id o184so8672193vsc.0 for ; Mon, 17 Aug 2020 10:19:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=t6dXdG+XRjoEkP9Gx+VAMgEHCOEEUrufqbt4td1Zg1c=; b=1zhWCCkI6wYVUyzgshV5lSr9LLnS3tj2gX9wLJm6la6Yu+p3A0nu4sibwagrqIEFr7 VTgqZTgdsTrK8DguchsPYQnW3+jp5kCklVBxhjXHkxx0SLsNnVdnGwITvoA+raTNd3xP iLMDr6Mh4q/SBh//tP5q4+0CaLlhUWxLt33a6hhlm4ED1Q5bYrFvVNY/7ObWkJVc6eca UGAUBzG2G5+9nlJLV35QL2513WCy36AF0HHPg5TTPj05N6Ycd/hT0iR6nfk1LWzR904J hssDvD+VDhzsiNPnTGqXaEkuahrb51olrmCfknlU/y6Lrn7/iCXYZHAZ6KYU7H+LZ8EK A7NA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=t6dXdG+XRjoEkP9Gx+VAMgEHCOEEUrufqbt4td1Zg1c=; b=iqJ2H1Vmc11812pDEN67KcsEi58HqAA89ARnCfvOMIZoBGJ1Y8Gyeutruh0aDrt4CR 8ye3naZqqrUk3m/KxSeCEEwR/MbW8UnBuJCEWD7o8ru7wwpWZcAmGRIIPyGST2ydceP+ aCRPJlQUzcjruba7ep+K59MeQ5N4DwRIuD5AEkEndPh6bkzCYqITwXdxuYNFeOR+T/a9 jeTVsjLj9zk24/ah8jMIngtqXqPIkppWc9F8AKMyAah12IOas7c17jDQBofosXs7GcyF gaf2v/2pv/Yw3nh+VB4v73ucXgpFJ9iP6SlaZkeRGe25q0Q6VdFRgKKiuAx9nbp3VkVy OUrA== X-Gm-Message-State: AOAM533Sdj+oEfOPta4vvixlTUWLYyqsyM4tEIMiRKxI8X5dSi2hOhou bsfGLxA2sebeiRX5DfI3Pjpm2MVNCxaM1zy1bVnWSMmw9d+epkMZ X-Google-Smtp-Source: ABdhPJw08pj620BdxKkMQ6R2UoWjtGpRDfgSjFn2P25jqY4twpiKLsb0H3oP40bwCY11seWfhKBv1e1MnxSCh/Sm9wM= X-Received: by 2002:a67:fc42:: with SMTP id p2mr8515018vsq.100.1597684770797; Mon, 17 Aug 2020 10:19:30 -0700 (PDT) MIME-Version: 1.0 References: <20200817164539.Horde.kYUsO4nuFe1V_fbKiAC6n5E@cloud.rochette.cc> In-Reply-To: <20200817164539.Horde.kYUsO4nuFe1V_fbKiAC6n5E@cloud.rochette.cc> Date: Mon, 17 Aug 2020 19:19:18 +0200 Message-ID: To: Mathieu Rochette Cc: "PHP internals (internals@lists.php.net)" Content-Type: multipart/alternative; boundary="0000000000003a1ca105ad15f9cc" Subject: Re: [PHP-DEV] Constructor parent property promotion From: andreas@dqxtech.net (Andreas Hennings) --0000000000003a1ca105ad15f9cc Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 wrote: > Hi, > > > > I=E2=80=99m writing a message here to receive feedback on a two ideas rel= ated 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=E2=80=99= s a lot > less of boilerplate when writing class constructors. I=E2=80=99m suggesti= ng two > additional ways of making thing even easier and I hope, readable. > > > > First: I=E2=80=99d like to be able to automatically transfer a constructo= r > property to its parent constructor. I=E2=80=99m thinking of something lik= e 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 examp= le > 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=E2=80=99d 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=E2=80=99ll be able to tackle. So, at that time if so= meone is > interested in mentoring this little project I=E2=80=99d appreciate it :) > > > > > > regards, > > > > Mathieu --0000000000003a1ca105ad15f9cc--