// php 链式操作实现原理
// class 普通实现
class DB
{
function all()
{
// 在需要链式操作的每个方法后面返回 calss 本身
return $this;
}
}
// __call 和 call_user_func_array 实现
class Foo
{
function __call($function, $args)
{
call_user_func_array([$this, $function], $args);
return $this;
}
}
然后就可以链式操作
链式操作return this
实现链式操作的核心:在方法最后返回 $this
$db->where('a')->order('b')->limit(1);
在方法里通过 ruturn $this 来完成链式操作。
$this->where()->order()->select();
通过ruturn $this来完成链式操作。
```php
$this->where()->order()->select();
```