【发布时间】:2013-04-16 18:32:35
【问题描述】:
我有一个框架(OpenCart)控制器类(如:catalog/controller/product/product.php)代码如下:
class ControllerProductProduct extends Controller {
public function index() {
//some code
$this->response->setOutput($this->render());
//some more code
}
}
有一个像$this->response->setOutput($this->render()); 这样的表达式。我知道这个表达式的用途,但我对它的工作原理很困惑。
$this 指的是当前类,即ControllerProductProduct,这意味着$this->response 对象必须存在于ControllerProductProduct 或其父类Controller 中。但这种情况并非如此。该对象实际上存在于父类Controller 的受保护属性中,如Controller::registry->data['response']->setOutput()。所以不应该这样说:
$this->registry->data['response']->setOutput();
而不是 $this->response->setOutput();
我还提供了Controller 类的 sn-p,以便您有想法。
abstract class Controller {
protected $registry;
//Other Properties
public function __construct($registry) {
$this->registry = $registry;
}
public function __get($key) {
//get() returns registry->data[$key];
return $this->registry->get($key);
}
public function __set($key, $value) {
$this->registry->set($key, $value);
}
//Other methods
}
我不知道这个表达式是如何工作的?知道这怎么可能吗?
谢谢。
【问题讨论】:
-
我有同样的麻烦,除了没有声明__get方法,它也可以使用,见:stackoverflow.com/questions/23183327/…
标签: php opencart magic-methods