检查Zend框架,我发现所有的setter方法(那些我已经研究)返回其居住的类的实例。它不仅设定值,但也返回$this
。 例如:
/* Zend_Controller_Router */
public function setGlobalParam($name, $value) {
$this->_globalParams[$name] = $value;
return $this;
}
/* Zend_Controller_Request */
public function setBaseUrl($baseUrl = null) {
// ... some code here ...
$this->_baseUrl = rtrim($baseUrl, '/');
return $this;
}
/* Zend_Controller_Action */
public function setFrontController(Zend_Controller_Front $front) {
$this->_frontController = $front;
return $this;
}
等等。 每一个公共的setter返回$this
。 而且它不仅制定者,也有其他的行动方法,返回$this
:
public function addConfig(Zend_Config $config, $section = null) {
// ... some code here ...
return $this;
}
这是为什么需要? 是什么返回$this
怎么办? 是否有一些特殊的意义?