PHP链式操作:
形如:$db->where()->order()->limit()的语法模式,在一行代码中完成多个方法的调用。链式操作的关键在于被调用的对象方法返回对象本身。
<?php
class Database {
private $sql;
public function where($where) {
$this->sql .= " where {$where}";
return $this;
}
public function order($order) {
$this->sql .= " order by {$order}";
return $this;
}
public function limit($limit) {
$this->sql .= " limit ({$limit})";
return $this;
}
public function go() {
return $this->sql;
}
}
$db = new Database();
$stmt = $db->where('id=1 and name=2')->order('id DESC')->limit(10)->go();
?>