您好,欢迎访问一九零五行业门户网

javascript - 如何用php实现类似js的链式操作?

$str = new str();//类似这种链式操作,如何实现?$str->str()->length();

回复内容: $str = new str();//类似这种链式操作,如何实现?$str->str()->length();

class myclass { public function dowork() { echo work done\n; return $this; } public function doanotherwork() { echo another work done\n; return $this; }}(new myclass())->dowork()->doanotherwork();
thinkphp的数据库操作就支持这种方式,你可以看看他的实现方法。
return $this;
https://segmentfault.com/a/1190000003069...
很简单,返回对象即可。
其它类似信息

推荐信息