Method chaining off constructors
| From: | Christoph Boget | Date: | Fri, 06 Jun 2008 14:45:11 +0000 |
| Subject: | Method chaining off constructors | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-275094@lists.php.net to get a copy of this message | ||
Is there a reason why you can't do method chaining off of constructors?
Consider the following class:
class bob
{
public function __construct()
{
echo 'Constructor()';
}
public function one()
{
echo '->one()';
return $this;
}
public function two()
{
echo '->two()';
return $this;
}
}
This works:
$bob = new bob();
$bob->one()->two();
whereas this doesn't.
$bob = new bob()->one()->two();
Why? I thought constructors returned the object?
thnx,
Christoph