Re: Method chaining off constructors

From: Date: Fri, 06 Jun 2008 15:04:53 +0000
Subject: Re: Method chaining off constructors
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-275097@lists.php.net to get a copy of this message
On Fri, Jun 6, 2008 at 11:00 AM, Christoph Boget <jcboget@yahoo.com> wrote: >> > Why? I thought constructors returned the object? >> It's been a while since I've played with objects in PHP, but couldn't >> you just add the line: >> return $this; >> ...to the end of your __construct() function? Sorry if this is obtuse of >> me to say, I just thought maybe the answer was that simple and you're >> like I am--you've been staring at a tree for so long, racking your >> brain, that you forget about the forest altogether. :) > > The constructor should already be returning $this. > > thnx, > Christoph > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > If you want to do this you need to define a function/method that returns an instance for you. So you can say class bob { public static function getInstance() { return new bob(); } public function foo() { } } bob::getInstance()->foo(); I've seen on the internals list where core dev's said it doesn't make sense to chain off a constructor. If you want this behavior then you need to do it off a method. This is just how things work is all it boils down to.

« previous php.general (#275097) next »