鏈式操作

php的鏈式操作在類裏面的方法中返回的是$this,即爲當前對象;

例如:

class Database {

    function where($where){
        echo $where . '--';
        return $this;
    }

    function limit($limit){
        echo $limit . '--';
        return $this;
    }

    function order($order){
        echo $order . '--';
        return $this;
    }

}

$db = new Database;
$db->where('where1')->where('where2')->limit(2)->order('order');

我想不僅僅在php中是這樣實現,在其他語言中應該也是採用這種方式來實現的(猜想);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章