<?php
namespace DbTestForChain;
class DbTestForChainOp {
public function where( $where){
$this->where = $where;
return $this;
}
public function order( $order){
$this->order = $order ;
return $this;
}
public function limit( $limit){
$this->limit = $limit ;
return $this;
}
}
$db = new DbTestForChainOp();
$query_res = $db->where(['id','1'])->order(['oder_id','desc'])->limit(10);
var_dump($query_res);
/* 【结果】
object(DbTestForChain\DbTestForChainOp)#1 (3) {
["where"]=>
array(2) {
[0]=>
string(2) "id"
[1]=>
string(1) "1"
}
["order"]=>
array(2) {
[0]=>
string(7) "oder_id"
[1]=>
string(4) "desc"
}
["limit"]=>
int(10)
}
*/
php面向对象【实现链式操作 Chain-Operation】
本文介绍了一种使用PHP实现的链式数据库查询方法。通过定义一个DbTestForChainOp类,支持where、order及limit等操作,实现了链式调用。示例展示了如何设置查询条件、排序方式以及限制返回结果的数量。

被折叠的 条评论
为什么被折叠?



