Re: RFC: constructor argument promotion

From: Date: Thu, 08 Aug 2013 13:12:01 +0000
Subject: Re: RFC: constructor argument promotion
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Le 08/08/2013 13:11, Martin Keckeis a écrit :
Am 08.08.2013 12:34 schrieb "Jordi Boggiano" <[email protected]>:
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); }

Thread (18 messages)

« previous php.internals (#68442) next »