Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103844 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 51775 invoked from network); 25 Jan 2019 14:27:15 -0000 Received: from unknown (HELO mail-io1-f65.google.com) (209.85.166.65) by pb1.pair.com with SMTP; 25 Jan 2019 14:27:15 -0000 Received: by mail-io1-f65.google.com with SMTP id m19so7425255ioh.3 for ; Fri, 25 Jan 2019 03:05:37 -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=YRV2Jed7PuPEWEbr0F1pxXgnee+hUPhFEzDumjHXM14=; b=T1Yjd4wKnz5J9foCZonvqTEyulHukVMMm71RvfToQ3qW3cg4AGSM/4RuQa+WhZsgPM adbR/ppFORsAmvHiPpbOiuReExMOZC9/0qvXccgs+Z8aOd9n+e+h2fdxgKe5UlyCgFgO vjL+ZFq/eoJ+K9+1qU84slAe7I9lnbqGtAmD63Il06Hlfb2bWC97Lb+laD8fP4WKzgb/ 0/S9YfQ49FOKw/t3GX3fjFydbvRcQW+nYqKeJz9A319iFgj6CdY82T000dvZkM6QR9LU Er6Zg1rS2OOCJjoTgrw5qchN8bM92zxQVtGLr1Oqa3+Xh/27QhRiBGVZifOmDcnIUu7l WcgA== 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=YRV2Jed7PuPEWEbr0F1pxXgnee+hUPhFEzDumjHXM14=; b=TUSleKNe8lXeD1+Pd1Draep8AfeaRejw1N3A5eGaUPROtLCxaVhlC2w0QSB8FmT/Fv mOm6CWO05Rc5dbOENwfWpuDVgvjLRR0VRsn76+btRrBPgB7q6pxT+VPW8HvFXBUju4RD jiKK+D0Hsj3swFj4cmUwJwQzmPtZ7n3Id6A40ao2q/HDnTdCdlRHbBB7OnhIAgOIABk0 o50h4qjlDzoAlKQ1+w/1/iszJDqt9AUoGylC3ixLIj8K+IQpM5ah3luYurOvMcVN9svD xFs/o03PRv5fwf5royU9ifBqiBk/+iR5njZxoSIQhfBxi0YwVUCIvDX+BieBcwXNu6/D DIDg== X-Gm-Message-State: AHQUAuZuMs9UWU1I6f5ro6rvBLtUpHAX/4YOxfxWLHxqkLs8fVJG9X8B B5IiQ9mT5zEW2Xsda6JhcurSLXIl0v/CFv4mQ90= X-Google-Smtp-Source: AHgI3IZBN+AUQ7otgUNymLDHwi8RKIFYwL571U5v6JcZ8EWSUKSWczpOgR1se9KEl6IJZh1JnX9lBXAk+klLmbA/WXw= X-Received: by 2002:a5e:aa06:: with SMTP id s6mr5364749ioe.187.1548414337148; Fri, 25 Jan 2019 03:05:37 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 25 Jan 2019 12:05:21 +0100 Message-ID: To: Andrey O Gromov Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000087f0720580464e79" Subject: Re: [PHP-DEV] Proposal fo "Code-free constructors declaration" From: nikita.ppv@gmail.com (Nikita Popov) --00000000000087f0720580464e79 Content-Type: text/plain; charset="UTF-8" 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 --00000000000087f0720580464e79--