在一個類中有多個方法,當你實例化這個類,並調用方法時只能一個一個調用,類似:
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();
如果要實現鏈式調用,這要在方法的結束添加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();
以上這篇PHP實現鏈式操作的原理詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。