class CChain{
private $instance=null;
private $haserror=false;
public function __construct($instance) {
if(!method_exists($instance,'getError'))
die('Instance does not have a method getError().');
$this->instance=$instance;
}
public function __call($m,$a) {
if($this->haserror)
return $m=='getError'?$this->haserror:$this;
$this->haserror=&$this->instance->getError()?:false;
if($this->haserror)
return $m=='getError'?$this->haserror:$this;
$ret=&call_user_func_array(array(&$this->instance, $m),$a);
$this->haserror=&$this->instance->getError()?:false;
if($this->haserror)
return $m=='getError'?$this->haserror:$this;
if($m=='getError') return $this->haserror;
if($ret===null)
return $this;
return $ret;
}
public function __get($n) {
return $this->instance->$n;
}
public function __set($n,$v) {
$this->instance->$n=$v;
}
}
class test {
public $error=false;
public function getError() {
return $this->error;
}
public function setError($v) {
$this->error=$v;
}
public function m0() {
/* someting without return*/
}
public function m1() {
/* someting without return*/
}
public function m2($foo=null) {
if($foo) {
return $this->setError('error '.__METHOD__);
}
/* someting without return*/
}
}
$test=new CChain(new test);
print_r( $test->m0()->m1()->m2(1) );
echo($test->error);