在其他类中,PhpStorm可以识别__construct()函数,但在yaf控制器中不能识别init()初始化方法,导致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(){
}
}在本例中,我希望在go to declaration类中使用“$this->model->testDeclaration();”从测试函数到testDeclaration()函数。但不起作用。
PhpStorm告诉我:
找不到要去的声明
在这里我如何正确地使用“去声明”?
发布于 2017-04-11 09:27:29
在其他类中,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
https://stackoverflow.com/questions/43339820
复制相似问题