在Codeigniter中,我们可以:
$this->select('users')->orderBy('id')->limit(20)
我认为这种将方法相互附加的方式在我的简单类中对我来说非常有用,但是怎么做呢?
这称为流畅的界面.要实现它,该函数只需返回自身.由于对象是通过引用返回的,因此您可以将多个调用链接在一起:
class SomeClass
{
public function select($table)
{
// do stuff
return $this;
}
public function orderBy($order)
{
// do stuff
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)