Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103848 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 19515 invoked from network); 25 Jan 2019 18:59:21 -0000 Received: from unknown (HELO mail-lf1-f65.google.com) (209.85.167.65) by pb1.pair.com with SMTP; 25 Jan 2019 18:59:21 -0000 Received: by mail-lf1-f65.google.com with SMTP id c16so7228956lfj.8 for ; Fri, 25 Jan 2019 07:37:46 -0800 (PST) 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=A7DPJyTbM6L2D/+R+l8DmdYxnCZPNn75aooYYKWq3X8=; b=A9VCcqXLM19oT64c0BLdlFezz0OdyujCYF7nmByqfTvXnm7+ddG7hlo1ZzTRM9/Q9I 4QV9CLccjUnVpg9kzV4qGuqi6ps8USc9ELhdAIz/ykuCHVjgZm4qUStw5jXaDgsK1ol1 s2OeU8Ur5eONG6XUK1iOFTBT/wvsj8Mht7JSKH1PTOya1PsCz1sdIGYgAZNU/RWB4CbZ IUhYi27tfIZ3K4/+5VXFySaMY8nJenIJW7lPysj2HlQ9YRFylpS01nMko6RZUOSc6gdc nS2WY15SdGpbYXC2oMm1ut4gLDDkyY34/4tUtltS+AZlA6kiU378uKuli7DpvxaHoFTf DqIQ== X-Gm-Message-State: AJcUukfySJDyyfq8P5xNlX5QNs3VmtHDKNt3dSWRQs10oja4kz7Sgzd7 sw1Nf0VlGbOWhsSlx6cIRz52QjDm7hWl2iPUJrk= X-Google-Smtp-Source: ALg8bN6wifQm4lgvVtmHIjsqYAAkP8Z8fZZqW9LdR/JbPezcAHxp418kUkku4rcVxv8OBqkU+PliyuHKhrRQaCAF7Qk= X-Received: by 2002:a19:cc46:: with SMTP id c67mr8977765lfg.145.1548430665153; Fri, 25 Jan 2019 07:37:45 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 25 Jan 2019 08:37:28 -0700 Message-ID: To: Nikita Popov Cc: Andrey O Gromov , PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Proposal fo "Code-free constructors declaration" From: levim@php.net (Levi Morrison) On Fri, Jan 25, 2019 at 4:05 AM Nikita Popov wrote: > > On Fri, Jan 25, 2019 at 11:44 AM Nikita Popov wrote: > > > 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 > > > > To add to this: While I can totally understand the motivation to avoid the > property/constructor boilerplate (where the property name needs to be > repeated four times), I think that this is really solving the wrong > problem. The problem is the need to have a constructor at all. > > Both this RFC and the two alternatives mentioned above really target the > case of "dumb" constructors that don't really do anything beyond assigning > properties from ctor arguments. I think that that is better solved by > adding a first-class syntax for object construction. I have something > roughly like this in mind: > > class Point { > public int $x; > public int $y; > public int $z; > } > > // (syntax just a dummy) > $origin = new Point { x = 0, y = 0, z = 0 }; > > Where the object initialization syntax ensures that all properties that > don't have defaults are initialized. Especially now that we have typed > properties and it is feasible to have data objects with just public typed > properties, I think that this is the way to avoid ctor boilerplate, rather > than making it simpler to write that boilerplate. > > Nikita I support this idea, although I think the `=` should be `=>` like arrays, or `:` like JSON. If you have any more complex of a need (such as private variables) then I think you should write the constructor. The reason is that if you have private variables, you need to establish a public API contract.