Zer*_*rbu 2 php oop methods nested class
例如,我见过第三方应用程序具有以下功能:
$db->select('columns')->from('table')->where('condition');
Run Code Online (Sandbox Code Playgroud)
这只是一个例子.你如何创建这样的方法?
为此,每个方法都应该返回$this
一个包含方法的类的实例.
class MyClass {
public function select($x){
// do something
return $this;
}
public function from($x){
// do something
return $this;
}
public function where($x){
// do something
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)
在这些方法中,您通常会对对象的状态执行某种修改.