首頁  >  文章  >  後端開發  >  PHP如何實作鍊式操作的原理

PHP如何實作鍊式操作的原理

*文
*文原創
2018-01-02 14:06:371211瀏覽

PHP如何實作鍊式運算?今天就為大家帶來一篇PHP實作鍊式運算的原理詳解。分享給大家,給大家做個參考。希望對大家有幫助。

在一個類別中有多個方法,當你實例化這個類,並調用方法時只能一個一個調用,類似:

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 底層的運作機制與原理

##php如何計算IP位址遮罩

php如何產生mysql資料字典#

以上是PHP如何實作鍊式操作的原理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn