Ker*_*nes 2 php object method-chaining
我见过其他对象这样做:
$obj->method1()->method2();
Run Code Online (Sandbox Code Playgroud)
我怎么做?每个函数只是修改对象的指针还是返回指针?
我不知道这种风格的正确用语 - 如果有人能帮助我,那就太棒了.
这是通过$this在每个函数的末尾返回来实现的,从而给出可链接的引用.
class MyClass {
public function method1() {
//...
return $this;
}
public function method2() {
//...
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)