在一个类中有多个方法,当你实例化这个类,并调用多个方法,类似:hello($value)->page('Page Two')->json();
<?php
class hello{
private $value;
public function __construct($value){
$this->value = $value;
}
public function page($title){
$this->value['content'] = $title;
return $this;
}
public function data(){
return $this->value;
}
public function json(){
echo json_encode($this->value);
}
public function html(){
print_r($this->value;)
}
}
写入数据测试一下:
// 测试数据
$value = array('title'=>'Hello world!');
// 链式方法一
$hello = new hello($value);
$hello->page('Page One')->html();
除了上面这种链式样式外,我们在如Lavarel中还会见到下面这种:
// 链式方法二
// 增加的代码
function hello($str){
return new hello($str);
}
hello($value)->page('Page Two')->json();
转载请注明出处:https://www.onexin.net/php-chain-operation/