Re: [PHP] Re: Constructor usage
On Sun, 04 Apr 2010 17:46:19 -0600, Nathan Rixham wrote: Larry Garfield wrote: Hi folks. Somewhat philosophical question here. I have heard, although not confirmed, that the trend in the Java world in the past several years has been away from constructors. That is, rather than this: class Foo { public void Foo(Object a, Object b, Object c) {} } Foo f = new Foo(a, b, c); The preference is now for this: class Foo { public void setA(Object a) {} public void setB(Object b) {} public void setC(Object c) {} } Foo f = new Foo(a, b, c); f.setA(a); f.setB(b); f.setC(c); I suppose there is some logic there when working with factories, which you should be doing in general. However, I don't know if that makes the same degree of sense in PHP, even though the OO models are quite similar. So, I'll throw the question out. Who uses example 1 above vs. example 2 when writing dependency-injection-based OOP? Why? What trade-offs have you encountered, and was it worth it? Other than theoretical reasons, one practical reason to have getters and setters is to make live easier for IDE's. I never thought the above two are in conflict. Usually parameterized constructors are provided along with getters and setters - each is used for good reasons. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Constructor usage
Larry Garfield wrote: > Hi folks. Somewhat philosophical question here. > > I have heard, although not confirmed, that the trend in the Java world in the > past several years has been away from constructors. That is, rather than > this: > > class Foo { > public void Foo(Object a, Object b, Object c) {} > } > > Foo f = new Foo(a, b, c); > > The preference is now for this: > > class Foo { > public void setA(Object a) {} > public void setB(Object b) {} > public void setC(Object c) {} > } > > Foo f = new Foo(a, b, c); > f.setA(a); > f.setB(b); > f.setC(c); > > I suppose there is some logic there when working with factories, which you > should be doing in general. However, I don't know if that makes the same > degree of sense in PHP, even though the OO models are quite similar. > > So, I'll throw the question out. Who uses example 1 above vs. example 2 when > writing dependency-injection-based OOP? Why? What trade-offs have you > encountered, and was it worth it? Hi Larry, In the Java world a huge reason is because Classes have to be able to be instantiated with no arguments in order to reverse engineered w/ JAXB so that the classes can be used for web services (and wsdl's created etc). Thus most of them have no arguments. Personally I also find it good practise to instantiate with no arguments and then set the state of the instance by calling setters. A nice way around it is to create static methods which instantiate the class w/ a protected or private constructor. class Foo { private $a; private function __construct() {} public function setA( $a ) { $this->a = $a; } public static function instantiateWithASet( $a ) { $temp = new self; $temp->setA( $a ); return $temp; } } it's almost overloading lol. Regards! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php