abstrak:下面小編就為大家?guī)硪黄狿HP實現(xiàn)鏈式操作的原理詳解。在一個類中有多個方法,當(dāng)你實例化這個類,并調(diào)用方法時只能一個一個調(diào)用,類似:db.php<?php class db { public function where() { //code here } public function order() { //code
下面小編就為大家?guī)硪黄狿HP實現(xiàn)鏈式操作的原理詳解。
在一個類中有多個方法,當(dāng)你實例化這個類,并調(diào)用方法時只能一個一個調(diào)用,類似:
db.php
<?php class db { public function where() { //code here } public function order() { //code here } public function limit() { //code here } }
index.php
<?php $db = new db(); $db->where(); $db->order(); $db->limit();
如果要實現(xiàn)鏈式調(diào)用,這要在方法的結(jié)束添加return $this即可。
db.php
<?php class db { public function where() { //code here return $this; } public function order() { //code here return $this; } public function limit() { //code here return $this; } }
index.php
<?php $db = new db(); $db->where()->order()->limit();
更多關(guān)于PHP實現(xiàn)鏈式操作的原理詳解請關(guān)注PHP中文網(wǎng)(m.miracleart.cn)其它文章!