Re: Method chaining off constructors

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx> wrote:

> On Fri, Jun 6, 2008 at 11:00 AM, Christoph Boget <jcboget@xxxxxxxxx>
> 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
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux