Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103843 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 46069 invoked from network); 25 Jan 2019 14:06:28 -0000 Received: from unknown (HELO mail-it1-f178.google.com) (209.85.166.178) by pb1.pair.com with SMTP; 25 Jan 2019 14:06:28 -0000 Received: by mail-it1-f178.google.com with SMTP id c9so9056758itj.1 for ; Fri, 25 Jan 2019 02:44:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=zHXsQaRicnMmKcILUwWyQv16bFTt/5RyVcRaZNO5BHI=; b=pI5HlZX/VVDnGvkepH/1QbHiHpTMTFqsEph54XKwS/dzq0h73Gl89bcJDJ9sI8gpRE JL3Lzm2X3KRIp5PQgsbqGTpDSIfcaUh9E7qDKkLzyGm60WRTy4MKQXLYDOm7NmwxFiZV xMlDRDbPhYY5JhMTSrsbUjN6yWo6Z0QU7Q0JYyyogs/bD43T1PBAWN6U1LSXJerJTXsS 1zzQJwSZnV8QKDqxxchiHiJRBN51TbxC2CSL/8m1ri6ESwREoGnVnW2QjzuxrI4RXeza NnYLul/tfePCzMhbgo+rdxmwSZ9QDB2F8FLL1wl3YKzpwUaFvhv5d/tPs6tR+GrCE76p uivQ== 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=zHXsQaRicnMmKcILUwWyQv16bFTt/5RyVcRaZNO5BHI=; b=cr5NBt/hC6GqyZMRO1njE5v8hT2aHvDD8f6F5gVirl1vltavAam80uDSxh49SN/3S2 7bNz9bmnQCspbtsBsha6LRVUsv2+5devuSYRHmTIlggNGG3S9qMLxpVXCfZNRVXm1rLG tFmeMlvaDsrIqzKIGE1aDO68hQzyjfOBY0iMuxsveVWSOK14xIzNqOJ6SsrzZRV7tbt3 ZoEY4GNNN3ubG2GOSwwILcRpDGxtSBZijywTIG1SFG7tlrh+yFZ7O2uwqY92DsBbasxF ttLj+WGlocnBs5FrAhcvyy2O7xNCdSrqCrKsIAUXI+pFdmHcEaCoACdhUcva2x4vhbd4 Kmyg== X-Gm-Message-State: AJcUukdA1g9PZ/wRIdkn0xnn9yVmxp6/dBsNqLVzXoUIPyZHT/pqb6WR zc3zucElYW55If3G7jqIhcq3HBZaiKjz7i5DNY8EHi2/ X-Google-Smtp-Source: ALg8bN7g12PsMnw+30TTm5Z6rzD6RYQSZy7tb1gkpLzWHbR4fOONZM4NH2pDlTjZEIFZkSTAFEwAOsnRXLvivrmACWQ= X-Received: by 2002:a05:660c:81a:: with SMTP id j26mr3797327itk.70.1548413089339; Fri, 25 Jan 2019 02:44:49 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 25 Jan 2019 11:44:33 +0100 Message-ID: To: Andrey O Gromov Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000027dec7058046046f" Subject: Re: [PHP-DEV] Proposal fo "Code-free constructors declaration" From: nikita.ppv@gmail.com (Nikita Popov) --00000000000027dec7058046046f Content-Type: text/plain; charset="UTF-8" On Wed, Jan 23, 2019 at 1:33 PM Andrey O Gromov wrote: > Full description > https://wiki.php.net/rfc/code_free_constructor > Draft realisation > https://github.com/php/php-src/compare/master...rjhdby:constructor > > "Code free" constructor is constructor with only purpose to directly set > object properties from received parameters and, optionally, call parent > constructor. > > Main idea is to move such constructor declaration inside class > declaration. > > Simple example: > > Current syntax > class A extends B{ > public $prop; > > public function __construct($prop){ > parent::__construct("BlaBla", $prop); > $this->prop = $prop; > } > } > > Proposed syntax > class A($prop) extends B("BlaBla", $prop) { > } > > With respect, Andrey. Two alternatives you might want to consider: * https://wiki.php.net/rfc/automatic_property_initialization => Proposed function public function __construct(int $this->x, int $this->y) {}, which avoids the need for explicit property assignments in the ctor. However, the property still needs to be declared separately. * https://docs.hhvm.com/hack/other-features/constructor-parameter-promotion => Uses public function __construct(public int $x, public int $y) {} to declare properties in-line in the constructor. I think that *if* we want to add some kind of sugar of this type, then I'd strongly prefer the syntax used by Hack than the one proposed here. It makes a lot more sense to me intuitively, probably because the property declarations still looks like normal property declarations, they just occur in-line in the ctor. Nikita --00000000000027dec7058046046f--