Christoph Boget schreef:
Constructors return the object, correct? If so, how can I do this: class Bob { private $blah; _construct( $blah ) { $this->blah = $blah; } public getBlah() { return $this->blah; } } echo Bob( 'Hello!' )->getBlah(); When I try that, I get the message "Undefined function Bob". I've also tried echo new Bob( 'Hello!' )->getBlah(); echo (new Bob( 'Hello!' ))->getBlah(); but PHP didn't like either of those at all. Is it just not possible what I'm trying to do?
class Foo { private $x; private function __construct($x) { $this->x = $x; } static function init($x) { return new self($x); } function double() { $this->x *= 2; return $this; } function triple() { $this->x *= 3; return $this; } function output() { echo $this->x, "\n"; } } Foo::init(2)->double()->triple()->output(); you can't chain of the constructor as Andrew explained. you may wish to return object clones to chain with as opposed to the same object - the example below is fairly bogus but it mgiht be helpful to you (btw run the code to see what it actually does as opposed to what you think it should do ... hey it caught me out and I wrote it!): class Foo2 { private $x; private function __construct($x) { $this->x = $x; } static function init($x) { return new self($x); } function double() { $this->x *= 2; return clone $this; } function triple() { $this->x *= 3; return clone $this; } function output() { echo $this->x, "\n"; } } $a = Foo2::init(2); $b = $a->double()->triple(); $a->output(); $b->output();
I'm using PHP5.2.1 thnx, Chris
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php