Re: How can I do this -- method chaining

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

 



On Jan 29, 2008 2:27 PM, Andrew Ballard <aballard@xxxxxxxxx> wrote:

> On Jan 29, 2008 1:53 PM, Christoph Boget <christoph.boget@xxxxxxxxx>
> wrote:
> > Constructors return the object, correct?
>
> Actually, I don't think so. I believe constructors return void, while
> the 'new' keyword returns a copy of the object.


im pretty sure constructors return an object instance:
php > class Test { function __construct() {} }
php > var_dump(new Test());
object(Test)#1 (0) {
}

but anyway, how could you even test that __construct() returned void
and the new keyword returned a copy of the object?  new essentially
invokes __construct() and passes along its return value, near as i can tell.

Christoph,
if you dont want to write a function in the global namespace, as suggested
in the article, Eric posted, just add a simple factory method in your class,
eg.
<?php
class Test {
    public static function getInstance() {
        return new Test();
    }

    public function doSomething() {
        echo __METHOD__ . PHP_EOL;
    }
}
Test::getInstance()->doSomething();
?>

-nathan

[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