方法@返回属性的子类
我有一个抽象类foo
和一个摘要建造者foobuilder
abstract class Foo {
}
abstract class FooBuilder {
protected Foo $model;
/**
* Return the class instance
*
* @return Model //What is the correct return type??
*/
public function get()
{
return $this->model;
}
}
我想在我的孩子建造者中使用方法get()
返回类型是子类,而不是抽象foo
。
class Bar extends Foo {
}
abstract class BarBuilder {
public function __construct()
{
$this->model = new Bar();
}
}
$barBuilder = BarBuilder();
$bar = $barBuilder->get(); //The type is "Bar", but IDE thinks is "Foo"
有什么方法可以返回phpdoc中的静态属性的静态类型?类似@return static($ this-> model)
之类的东西?
一个示例是Laravel雄辩在somemodel :: find()
中所做的。 IDE知道类型可以是somemodel
。但是@return
只有模型
。
I have a abstract class Foo
and a abstract builder FooBuilder
abstract class Foo {
}
abstract class FooBuilder {
protected Foo $model;
/**
* Return the class instance
*
* @return Model //What is the correct return type??
*/
public function get()
{
return $this->model;
}
}
I want to use the method get()
in my child Builders and the IDE detect that the return type is the child class, not the abstract Foo
.
class Bar extends Foo {
}
abstract class BarBuilder {
public function __construct()
{
$this->model = new Bar();
}
}
$barBuilder = BarBuilder();
$bar = $barBuilder->get(); //The type is "Bar", but IDE thinks is "Foo"
Is there any way of returning a static type of a attribute not the class in PHPDoc? Something like @return static($this->model)
?
An example is what Laravel's Eloquent does in SomeModel::find()
. The IDE knows that the type can be SomeModel
. But the @return
only have Model
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在DocBlock中使用仿制药确实有助于phpstorm(我的是2021.2.2):
Using generics in docblock does help PhpStorm(mine is 2021.2.2):
您可能应该将FOO用作示例中的返回类型;但是为了娱乐,您可以使用静态返回类型来确定下面的子实例
You should probably use Foo as the return type in your example; but for fun, you can use static return type to determine child instance like below