在其他类中,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(){ } }在这个例子中,我想从测试函数$this->model->testDeclaration();使用' go to declaration ' $this->model->testDeclaration(); testDeclaration()类中的testDeclaration()函数。 但它不起作用。
PhpStorm告诉我:
找不到申报单
我怎样才能在这里正确使用'去声明'?
In other class, PhpStorm can identify __construct() function, but in yaf controller it can not identify the initialization method init(), resulting in the init() can not trace initialization operation.
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(){ } }In this example, I want use 'go to declaration' from test function $this->model->testDeclaration(); to testDeclaration() function in TestModel class. But it does not work.
PhpStorm tells me:
Cannot find declaration to go to
How can I use 'go to declaration' correctly here?
最满意答案
在其他类中,PhpStorm可以识别__construct()函数,但是在yaf控制器中它无法识别初始化方法init() ,导致init()无法跟踪初始化操作。
PhpStorm对__constructor()有特殊处理 - 如果它在方法体内有任何赋值操作,它会跟踪类变量/属性将获得什么。
例如,在此代码中,它知道$this->model将是TestModel类的实例 - IDE甚至在__construct()方法体之外保留此信息。
对于其他方法,例如你的case中的init() ,这样的信息会被丢弃到外面(所以它只是方法体的本地信息)。
您可以通过使用带@var标记的简单PHPDoc注释轻松解决此问题,您将为model属性提供类型提示:
/** @var \TestModel Optional description here */ private $model;养成为所有属性/类变量提供类型提示的习惯,即使IDE自动检测其类型 - 它可以帮助IDE长期运行。
https://phpdoc.org/docs/latest/references/phpdoc/tags/var.html
In other class, PhpStorm can identify __construct() function, but in yaf controller it can not identify the initialization method init(), resulting in the init() can not trace initialization operation.
PhpStorm has special treatment for __constructor() -- it tracks what type class variable/property will get if it will have any assignment operations within the method body.
For example, in this code it knows that $this->model will be instance of TestModel class -- IDE keeps this info even outside of the __construct() method body.
For other methods, like init() in your case, such info gets discarded outside (so it's local to the method body only).
You can easily resolve this by using simple PHPDoc comment with @var tag where you will provide type hint for model property:
/** @var \TestModel Optional description here */ private $model;Make a habit of providing type hint for all properties/class variables, even if IDE autodetects its' type -- it helps IDE in long run.
https://phpdoc.org/docs/latest/references/phpdoc/tags/var.html
更多推荐
发布评论