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