Zend 框架 1.12。是否可以在函数 init() 中定义对象并将其用作全局对象

Zend Framework 1.12. Is it possible to define object inside function init() and use it as global object

如果我像这样在函数 init() 中调用对象,我将无法使用自动完成功能:

class IndexController extends HiNStudio_Controller {

private $model_obj;

public function init() {
    $this -> model_obj = new Default_Model_Account();
}

public function indexAction() {
    $this -> model_obj ->  //(it should be something autocomplete here)
}

如果我调用正确的函数名称,代码仍然有效。例如:

public function indexAction() {
    $this -> model_obj ->  checkLogin();
}

是否有任何情况显示自动完成功能而不是像这样在每个函数中调用对象:

class IndexController extends HiNStudio_Controller {

private $model_obj;

public function init() {

}

public function indexAction() {
    $this -> model_obj = new Default_Model_Account();
    $this -> model_obj ->  //(autocomplete is working here)
}

我对PHP不太熟悉,但我认为它与方法调用model_obj无法初始化有关。尝试用构造函数替换函数 init。 http://php.net/manual/en/language.oop5.decon.php 这意味着当您创建 IndexController 构造函数的新对象时,将始终设置 model_obj 变量。

使用 PHPDoc 注释提示您的变量。

/** @var Default_Model_Account */
private $model_obj;

如果未在构造函数中初始化或未在初始化的同一方法中使用,PhpStorm 会丢失变量类型。