PHP设计模式(6)- PHP链式操做

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(); ?>
相关文章
相关标签/搜索