Re: How can I do this -- method chaining
| From: | Jochem Maas | Date: | Wed, 30 Jan 2008 13:12:42 +0000 |
| Subject: | Re: How can I do this -- method chaining | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-268320@lists.php.net to get a copy of this message | ||
Anup Shukla schreef:
Nathan Nobbe wrote:not exactly - 'new' asks php to initialize an object of the given class, the 'binding' to a variable occurs because of the assignment operator. the __construct() method is called automatically by php after the object structure has been initialized, so primarily nothing is returned because the call to __construct() doesn't happen directly in userland code. at least that's how I understand it.AFAIK, constructor simply constructs the object, and *new* is the one that binds the reference to the variable on the lhs.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) { }
So, constructors return nothing.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