让自己的代码风格更进一步!!!
我们经常在项目中会写到
Mode::find()->where()->orderBy()->limit();
链式调用,那么它是怎么实现的呢?
关键点在于类中方法返回的 return $this;
代码如下
<?php
class BaseObject
{
public $where;
public $limit;
function where($condition)
{
$this->where = $condition;
return $this;
}
function limit($limit)
{
$this->limit = $limit;
return $this;
}
}
$model = new BaseObject();
$model->where(['id' => 1])->limit(10);