php鏈式操作的實現(xiàn)方式分析
本文實例講述了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相關內(nèi)容感興趣的讀者可查看本站專題:《php面向對象程序設計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
PHP使用pdo連接access數(shù)據(jù)庫并循環(huán)顯示數(shù)據(jù)操作示例
這篇文章主要介紹了PHP使用pdo連接access數(shù)據(jù)庫并循環(huán)顯示數(shù)據(jù)操作,結合實例形式較為詳細的分析了php使用pdo進行access數(shù)據(jù)庫連接、查詢、執(zhí)行sql語句、預處理等相關操作技巧與注意事項,需要的朋友可以參考下2018-06-06

