[轉]让PHP支持像jQuery那样的链式操作

From : http://x1989.com/a/94.html

群里有人讨论这个问题,我的想法是在方法里返回this指针(指向对象本身),通过对私有成员$sql的赋值,来获得最终的执行语句,并返回相应结果。

Class Database{
private $sql;
public function where($condition){
//此处省略n行...
return this;
}
public function limit($limit){
//此处省略n行...
return this;
}
public function execute(){
return this->query($sql);
}
public function select(){
//此处省略n行...
return this;
}
}
//调用时:
$DB = new Database();
$DB->where($condition)->limit(20)->select();

 

posted @ 2011-11-09 10:59  Athrun  阅读(287)  评论(0编辑  收藏  举报