在使用框架的過程中,我們經常遇到過以下這種語句
$database->where("id = 1")->order("id desc")->limit(10);這種操作被稱為鏈式操作。
實現上述操作只需要掌握一種技巧,比如:
Database.php
<?phpclass Database{ function where($where) { } function order($order) { } function limit($limit) { }}test.php
include "Database.php"$database = new Database();$database->where("id > 10");$database->order("id desc");$database->limit(10);如要實現鏈式操作,只需要修改Database.php
class Database{ function where($where) { return $this;//重點 } function order($order) { return $this; } function limit($limit) { return $this; }}test.php
$database->where("id = 1")->order("id desc")->limit(10);新聞熱點
疑難解答
圖片精選