使用$this实现独特的调用方式

来源:互联网 发布:b50的座子淘宝怎么没有 编辑:程序博客网 时间:2024/04/28 17:45

使用过thinkphp都知道其curd的连贯操作方式:

$model->where($map)->limit()->select();

这么调用是不是很酷^_^

其实where、limit、select是类中普通的方法,不同的是中间的那些加了一个return $this

这种方式常用的场景是通过方法设置参数或者处理(不需要返回值)

 

<?phpclass example {var $where;var $limit;function where($where) {$this->where = $where;return $this;}function limit($limit) {$this->limit = $limit;return $this;}function select() {print("where: {$this->where},limit: {$this->limit}");}}$model = new example;$model->where("id=12 and cid=1")->limit("(0,5)")->select();?>


 

原创粉丝点击