Re: Method chaining off constructors
| From: | Adam Richardson | Date: | Fri, 06 Jun 2008 15:26:29 +0000 |
| Subject: | Re: Method chaining off constructors | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-275098@lists.php.net to get a copy of this message | ||
The 'new' keyword has to apply to the object created in the constructor (and
not the return value of any of the follow-up calls.) To establish this
precedence, chaining wasn't allowed on constructors.
On Fri, Jun 6, 2008 at 11:04 AM, Eric Butera <eric.butera@gmail.com> wrote:
> 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.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>