php 对象中连贯执行方法

连贯操作的重点是返回当前对象,以便可以继续执行

class Ceshi{
    
    public $str = '';
    
    public function f1($a){
        $this->str .= $a;
        //连贯操作的重点是返回当前对象,以便可以继续执行
        return $this;
    }
    public function f2($b){
        $this->str .= $b;
        return $this;
    }
    public function get(){
        return $this->str;
    }
}

$MyClass = new Ceshi();
echo $MyClass->f1(1)->f2(2)->get();

 

posted on 2016-10-23 08:50  鱼塘总裁  阅读(257)  评论(0编辑  收藏  举报