Esame di Zend Framework Ho scoperto che ogni metodo di setter di qualsiasi classe (di quelli che ho esaminato) restituisce l'istanza della classe in cui vive. Non solo imposta un valore ma imposta un valore e restituisce anche $ this. Ad esempio:
/* 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;
}
E così via. Ogni setter pubblico restituisce $ this. E non solo setter, ci sono anche alcuni metodi d'azione, che restituisce anche $ this:
public function addConfig(Zend_Config $config, $section = null) {
// ... some code here ...
return $this;
}
Quello che per questo è necessario? Cosa restituisce $ questo? Ha qualche significato speciale?
Tornando '$ (this)' significa che è possibile metodi di catena, ad esempio '$ myClass-> method_1 ($ x) -> method_2 ($ y)'. –
Ho notato questo anche in passato e non ho mai pensato di porre la domanda. Grazie ora ho la risposta. – RockyFord