本文實例講述了php鏈式操作的實現(xiàn)方式。分享給大家供大家參考,具體如下:
類似$db->where("id=1")->limit("5")->order("id desc")
,鏈式操作的實現(xiàn)方式
先講下方法的常規(guī)調用;
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();
缺點:實現(xiàn)多個方法需要多行調用;
鏈式操作,在方法返回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數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:- PHP實現(xiàn)的連貫操作、鏈式操作實例
- PHP簡單數(shù)據(jù)庫操作類實例【支持增刪改查及鏈式操作】
- PHP三種方式實現(xiàn)鏈式操作詳解
- PHP對象鏈式操作實現(xiàn)原理分析
- PHP實現(xiàn)鏈式操作的核心思想
- PHP實現(xiàn)鏈式操作的原理詳解
- PHP實現(xiàn)鏈式操作的三種方法詳解
- php類自動裝載、鏈式操作、魔術方法實現(xiàn)代碼
- PHP封裝類似thinkphp連貫操作數(shù)據(jù)庫Db類與簡單應用示例
- thinkPHP5框架數(shù)據(jù)庫連貫操作之cache()用法分析
- thinkphp連貫操作實例分析