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