2.11. 流接口模式(Fluent Interface)

未匹配的标注

目的

链式调用,看起来很爽,好吧 这是来混章节数的。

代码实现

<?php
class Replace{
    public $str = '';
    public function ltrim()
    {
        $this->str = str_replace("www","xxx",$this->str);
        return $this;
    }

    public function addWww()
    {
        $this->str .= '來源:http://xxx.xxx'.PHP_EOL;
        return $this;
    }
    public function setStr($str)
    {
        $this->str = $str;
        return $this;
    }
    public function get()
    {
      return $this->str;
    }
}

$replace = new Replace();

$str = 'www';
echo $replace->setStr($str)->ltrim()->addWww()->get();

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~