PHPStorm类型提示子类的基类 [英] PHPStorm type hinting subclasses of baseclass
问题描述
关于此帖子:
在PHPStorm中,当我有一个超类方法返回每个子类的不同类型时,如何使类型提示起作用,
这是关于PHPStorm类型提示中的一种极端情况. 请尝试遵循-我会尽力做到尽可能清楚:
this one is about an edge case in PHPStorm type hinting. Please try to follow along - I'll do my best to be as clear as possible:
所以,我有了这个基本的抽象类:
So, I've got this base abstract class:
abstract class myBaseController {
protected $_model;
...
}
另一个类继承自的内容:
which another class inherits from:
class myController extends myBaseController {
$hello = 'hello';
...
}
,并由第三类进一步扩展:
and which is further extended by a third class:
class myNewController extends myController {
public $myvar;
$this->_model = new myModel();
...
public function myFunc(){
// !!form is underlined as: "Method 'form' not found in class"!!
$form = $this->_model->form($new_variable);
}
下面是myModel类的示例:
Below is a sample of the myModel class:
class myModel extends BaseModel {
$world = 'world';
public function form($my_variable) {
do_something();
}
我的真正问题是如何在这种情况下正确"phpdoc":
My true question is how to properly "phpdoc" this scenario:
子类_model
来分配另一个具有唯一功能form
的类myModel
的实例. PHPStorm应该如何正确找到myNewController
中的form
?
A subclass myNewController
is using an inherited variable _model
to assign an instance of another class myModel
which has a unique function form
. How should PHPStorm properly find out about form
in myNewController
?
到目前为止,我的解决方案包括像这样记录myBaseController
:
My solution so far involves documenting myBaseController
like this:
abstract class myBaseController {
/**
* @var object
*/
protected $_model;
...
}
但是我认为@var object
的范围太广(PHPStorm找不到其声明),我的猜测是应该有一种更好(更具体)的方法来实现此目的.
However I think @var object
is too broad (PHPStorm won't find its declaration) and my guess is that there should be a better (and more specific) way to do this.
也许我们可以代替:
/**
* @var BaseModel
*/
如果PHPStorm可以查看该方法的子类.
if PHPStorm had a way of looking into the subclasses for the method.
有什么想法吗? 谢谢大家.
Any ideas? Thank you all in advance.
推荐答案
您可以使用@property
批注在子类中指定属性类型,而无需引入新代码:
You can specify the property type in your subclass without introducing new code, using the @property
annotation:
/**
* @property myModel $_model
*/
class myNewController extends myController
这篇关于PHPStorm类型提示子类的基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!