是否可以知道方法调用是否来自方法链?
例如,我有以下内容class
:
class Test{
protected $string = '123';
public function a($string){
$this->string .= $string;
if(method chain){
return $this;
}else{
return $this->string;
}
}
public function b($string){
$this->string .= $string;
if(method chain){
return $this;
}else{
return $this->string;
}
}
}
Result:
$test = new Test();
echo $test->a('000'); // 123000
echo $test->a('000')->b('www'); // 123000www
UPDATE
我最终创建了一个exec()
方法告诉没有更多的方法将被调用。
public function exec(){
return $this->string;
}
这是不可能的,因为您不知道将使用方法结果的上下文。
相反,您可以随时返回$this
正当使用__toString http://php.net/manual/en/language.oop5.magic.php#object.tostring方法返回你的$string
:
class Test{
protected $string = '123';
public function a($string){
$this->string .= $string;
return $this;
}
public function b($string){
$this->string .= $string;
return $this;
}
public function __toString() {
return $this->string;
}
}
然后,如果您回显您的值 - 它会将其用作字符串,否则您将处理一个对象。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)