Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:68442 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 15536 invoked from network); 8 Aug 2013 13:12:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Aug 2013 13:12:08 -0000 X-Host-Fingerprint: 90.53.66.174 ALyon-654-1-298-174.w90-53.abo.wanadoo.fr Received: from [90.53.66.174] ([90.53.66.174:19916] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id ED/E4-06453-42993025 for ; Thu, 08 Aug 2013 09:12:05 -0400 Message-ID: To: internals@lists.php.net Date: Thu, 08 Aug 2013 15:12:01 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130803 Thunderbird/17.0.8 MIME-Version: 1.0 References: <52037411.7070406@seld.be> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Posted-By: 90.53.66.174 Subject: Re: [PHP-DEV] RFC: constructor argument promotion From: matthieu@mnapoli.fr (Matthieu Napoli) Le 08/08/2013 13:11, Martin Keckeis a écrit : > Am 08.08.2013 12:34 schrieb "Jordi Boggiano" : >> >> On 08.08.2013 10:34, Leigh wrote: >>> I'm not sure what problem this is really trying to solve, the > boilerplate >>> code you mention is very explicit and it is very clear to the reader > what >>> is being done. Each property documented with its type, purpose and >>> visibility in a common place for easy reference (at the top of the > class). >>> Each property that takes a value from the constructor assigned in the >>> constructor. Very clear. >> >> I for one am pretty tired of writing this boilerplate in every second >> class I write. Using dependency injection you end up having to write a >> LOT of those usually, and constructors typically only contain assignments. >> >> Adding a property means: declaring the property, adding the ctor arg, >> adding the assignment in the ctor. You have to write the property name 4 >> times. With this RFC in it'd come down to writing it once, and would >> avoid having undeclared properties because someone forgot. >> >> I am very supportive the idea, although I see that it could be confusing >> to some. Maybe the syntax needs to change, but the overall change is >> much welcome. > > The syntax itself i feel is self explaining. You read it and you know whats > going on. > > But somehow i feel this is "wrong". Declaring visibility directly in the > arguments? This looks very scary to me. (Maybe only because i've never seen > it before...) > > Other reasons against: > Phpdoc generator break > Code completion break > If constructor is not at the beginning never see a var declaration > Auto generation of set/get can be achieved with IDE..so its not much work I second the feeling about the syntax, there are too many disadvantages. However the idea is excellent. Maybe an alternative approach could do, here is a random suggestion (a bit more verbose): class MyClass { public $foo; protected $bar; public function __construct($this->foo, $this->bar, $baz) { // $this->foo and $this->bar are now set $baz->doAnything(); // $baz is a standard parameter } } This would even be compatible with an interface: interface MyInterface { function __construct($foo, $bar, $baz); }