用php實現一個簡單的鏈式操作

來源:互聯網
上載者:User
最近在讀《php核心技術與最佳實務》這本書,書中第一章提到用__call()方法可以實現一個簡單的字串鏈式操作,比如,下面這個過濾字串然後再求長度的操作,一般要這麼寫:

strlen(trim($str));

那麼能否實現下面這種寫法呢?

$str->trim()->strlen();

下面就來試下。

鏈式操作,說白了其實就是鏈式的調用對象的方法。既然要實現字串的鏈式操作,那麼就要實現一個字串類,然後對這個類的對象進行叫用作業。我對字串類的期望如下:(1)當我建立對象時,我可以將字串賦值給對象的屬性,並且可以訪問這個屬性讀取值;(2)我可以調用trim() 和strlen()方法;(3)我還可以這麼調用方法$str->trim()->strlen()。

上面的第(1)條,是一個字串類的基本要求。先把這個實現了:

class String{    public $value;    public function __construct($str=null)    {        $this->value = $str;    }}

可以試下:

1 $str = new String('01389');

2 echo $str->value;

然後再看第2條,先把$str->trim()實現了,參考書中的思路:觸發__call方法然後執行call_user_func。代碼如下:

class String{    public $value;    public function __construct($str=null)    {        $this->value = $str;    }    public function __call($name, $args)    {        $this->value = call_user_func($name, $this->value, $args[0]);        return $this;    }}

測試下:

1 $str = new String('01389');

2 echo $str->trim('0')->value;

結果如下:

上面需要注意的是第12行: $this->value = call_user_func($name, $this->value, $args[0]); $name是回呼函數的名字(這裡也就是trim),後面兩個是回呼函數(tirm)的參數,參數的順序不要弄顛倒了。$args是數組,也需要注意下。

第2條中還要實現strlen(),這時上面代碼中的第13行就很關鍵了: return $this; 它的作用就是,在第12行調用trim()處理完字串後重新value屬性賦值,然後返回當前對象的引用,這樣對象內的其他方法就可以對屬性value進行連續操作了,也就實現了鏈式操作。$str->strlen()實現如下:

class String{    public $value;    public function __construct($str=null)    {        $this->value = $str;    }    public function __call($name, $args)    {        $this->value = call_user_func($name, $this->value, $args[0]);        return $this;    }    public function strlen()    {        return strlen($this->value);    }}

測試下:

1 $str = new String('01389');

2 echo $str->strlen();

結果:

鏈式操作:

echo $str->trim('0')->strlen();

結果:

class String{    public $value;    public function __construct($str=null)    {        $this->value = $str;    }    public function trim($t)    {        $this->value = trim($this->value, $t);        return $this;    }    public function strlen()    {        return strlen($this->value);    }}

鏈式操作的關鍵是在做完操作後要return $this。

另外,本文受到園子裡這篇文章的啟發,用call_user_func_array()替換了call_user_func()實現,將__call()方法修改如下。

public function __call($name, $args)    {        array_unshift($args, $this->value);        $this->value = call_user_func_array($name, $args);        return $this;    }

與上面的__call()方法效果是相同的,這樣代碼似乎比之前的實現要優雅些。

總結:

__call()在對象調用一個不可訪問的方法時會被觸發,所以可以實作類別的動態方法的建立,實現php的方法重載功能,但它其實是一個文法糖(__construct()方法也是)。

那麼如果沒有__call()等文法糖,能否實現動態方法的建立和鏈式操作呢?我想會涉及到以下幾個方面的問題:類方法是否存在和可以調用,這個可以用method_exists、is_callable、get_class_methods等方法來實現,另外,就是在建立對象時給屬性賦值(初始化),這個文法糖確實方便,不過不是必需的。等有時間再研究下吧。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.