PHP中如何实现链式调用操作方法分析?
- 内容介绍
- 文章标签
- 相关推荐
本文共计500个文字,预计阅读时间需要2分钟。
本例展示了PHP链式操作实现方式。分享给家人参考,如下:
phpnamespace Com;
class Model { public $db; public function __construct() { $this->db=new PDO('mysql:host=localhost;dbname=test', 'root', ''); }
public function where($condition) { // 这里可以添加实现细节,如拼接条件等 }
public function limit($limit) { // 这里可以添加实现细节,如拼接限制等 }
public function order($order) { // 这里可以添加实现细节,如拼接排序等 }
public function fetchAll() { $sql='SELECT * FROM table WHERE 1'; if (!empty($this->db->where)) { $sql .=' AND ' . $this->db->where; } if (!empty($this->db->limit)) { $sql .=' LIMIT ' . $this->db->limit; } if (!empty($this->db->order)) { $sql .=' ORDER BY ' . $this->db->order; }
$stmt=$this->db->prepare($sql); $stmt->execute(); return $stmt->fetchAll(); }}
// 使用示例$model=new Model();$result=$model->where('id=1') ->limit(25) ->order('id DESC') ->fetchAll();
以上代码展示了如何通过链式调用方式实现条件过滤、限制、排序等功能,并执行查询操作。希望对您有所帮助。
本文实例讲述了php链式操作的实现方式。分享给大家供大家参考,具体如下:
类似$db->where("id=1")->limit("5")->order("id desc"),链式操作的实现方式
先讲下方法的常规调用;
namespace Com; class Database{ function where($where){ echo $where; } function order($order){ echo $order; } function limit($limit){ echo $limit; } }
调用
$db = new \Com\Database(); $db->where(); $db->limit();
缺点:实现多个方法需要多行调用;
链式操作,在方法返回return $this;即可使用链式操作;
namespace Com; class Database{ function where($where){ echo $where; return $this; } function order($order){ echo $order; return $this; } function limit($limit){ echo $limit; return $this; } }
使用链式调用:
$db = new \Com\Database(); $db->where("id=1")->limit("5")->order("id desc");
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
本文共计500个文字,预计阅读时间需要2分钟。
本例展示了PHP链式操作实现方式。分享给家人参考,如下:
phpnamespace Com;
class Model { public $db; public function __construct() { $this->db=new PDO('mysql:host=localhost;dbname=test', 'root', ''); }
public function where($condition) { // 这里可以添加实现细节,如拼接条件等 }
public function limit($limit) { // 这里可以添加实现细节,如拼接限制等 }
public function order($order) { // 这里可以添加实现细节,如拼接排序等 }
public function fetchAll() { $sql='SELECT * FROM table WHERE 1'; if (!empty($this->db->where)) { $sql .=' AND ' . $this->db->where; } if (!empty($this->db->limit)) { $sql .=' LIMIT ' . $this->db->limit; } if (!empty($this->db->order)) { $sql .=' ORDER BY ' . $this->db->order; }
$stmt=$this->db->prepare($sql); $stmt->execute(); return $stmt->fetchAll(); }}
// 使用示例$model=new Model();$result=$model->where('id=1') ->limit(25) ->order('id DESC') ->fetchAll();
以上代码展示了如何通过链式调用方式实现条件过滤、限制、排序等功能,并执行查询操作。希望对您有所帮助。
本文实例讲述了php链式操作的实现方式。分享给大家供大家参考,具体如下:
类似$db->where("id=1")->limit("5")->order("id desc"),链式操作的实现方式
先讲下方法的常规调用;
namespace Com; class Database{ function where($where){ echo $where; } function order($order){ echo $order; } function limit($limit){ echo $limit; } }
调用
$db = new \Com\Database(); $db->where(); $db->limit();
缺点:实现多个方法需要多行调用;
链式操作,在方法返回return $this;即可使用链式操作;
namespace Com; class Database{ function where($where){ echo $where; return $this; } function order($order){ echo $order; return $this; } function limit($limit){ echo $limit; return $this; } }
使用链式调用:
$db = new \Com\Database(); $db->where("id=1")->limit("5")->order("id desc");
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。

