mutator方法可用于链接方法,其中这些方法返回原始对象,而其他方法可在由mutator函数返回的这些对象上调用。
下面是一个简单的例子,展示了相同的-
<?php
class sample_class {
private $str;
function __construct() {
$this->str = "";
}
function addA() {
$this->str .= "am";
return $this;
}
function addB() {
$this->str .= "_bn";
return $this;
}
function getStr() {
return $this->str;
}
}
$new_object = new sample_class();
echo $new_object->addA()->addB()->getStr();
输出结果
这将产生以下输出-
am_bn