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

问题描述:

I cannot use autocomplete if I call an object inside function init() like this:

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)
}

pic

The code is still working if I call correct function name. Ex:

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

Is there any case to display autocomplete function instead of call Object inside each function like this:

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)
}

pic

Typehint your variable with PHPDoc comment.

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

PhpStorm looses variable type if it's not initialized in constructor or not used in the same method where initialized.

I am not that familiar with PHP but I think it is releated to that at the method call model_obj can be not initialized. Try to replace function init with constructor. http://php.net/manual/en/language.oop5.decon.php This would mean that when you create new object of IndexController constructor will always set model_obj variable.