PHP實作鍊式操作的原理

高洛峰
發布: 2016-10-10 10:05:56
原創
1208 人瀏覽過

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

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.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!