2017-04-11 195 views
0

在其他类中,PhpStorm可以识别__construct()函数,但是在yaf控制器中它不能识别初始化方法init(),导致init()无法跟踪初始化操作。PhpStorm不支持​​yaf的init方法

class TestController extends Yaf_Controller_Abstract{ 
    private $model; 
    public function init() { 
     $this->model = new TestModel(); 
    } 

    public function test(){ 
     $this->model->testDeclaration(); 
    } 
} 

class TestModel{ 
    public function testDeclaration(){ 
    } 
} 

在这个例子中,我想在TestModel类使用“go to declaration”自测试功能$this->model->testDeclaration();testDeclaration()功能。但它不起作用。

PhpStorm告诉我:

找不到声明去

如何使用 '去申报' 正确吗?

回答

1

在其它类,PhpStorm可以识别__construct()功能,但在YAF控制器它无法识别初始化方法init(),导致init()无法跟踪初始化操作。

PhpStorm对__constructor()有特殊处理 - 它跟踪哪些类型的变量/属性会在方法体内进行任何赋值操作。

例如,在此代码中,它知道$this->model将是TestModel类的实例 - IDE将此信息保存在__construct()方法主体之外。

对于其他方法,如init()在你的情况下,这些信息会被丢弃到外面(所以它只在方法体的本地)。


您可以轻松地通过使用简单的PHPDoc的评论与@var标签,你会为model属性提供类型提示解决此问题:

/** @var \TestModel Optional description here */ 
private $model; 

让所有属性/类变量提供类型暗示的习惯即使IDE自动检测其'类型 - 它可以长期帮助IDE。

https://phpdoc.org/docs/latest/references/phpdoc/tags/var.html

+0

非常感谢你,这样可以 –

+0

@张轩铭是否能解决您的问题,即考虑将其标记这个答案所接受。 – LazyOne