首頁  >  文章  >  类库下载  >  PHP實作鍊式操作的原理

PHP實作鍊式操作的原理

高洛峰
高洛峰原創
2016-10-10 10:05:561210瀏覽

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

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();


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

相關文章

看更多