Autocomplete 如何使PhpStorm中的继承方法的自动编译正常工作?
有两类定义如下:Autocomplete 如何使PhpStorm中的继承方法的自动编译正常工作?,autocomplete,ide,phpstorm,fluent-interface,Autocomplete,Ide,Phpstorm,Fluent Interface,有两类定义如下: class Foo { private $aaa; public function setAaa(Aaa $aaa): self { $this->aaa = $aaa; return $this; } } class Bar extends Foo { private $bbb; public function setBbb(Bbb $bbb): self { $t
class Foo
{
private $aaa;
public function setAaa(Aaa $aaa): self
{
$this->aaa = $aaa;
return $this;
}
}
class Bar extends Foo
{
private $bbb;
public function setBbb(Bbb $bbb): self
{
$this->bbb = $bbb;
return $this;
}
}
所以这里使用了“流畅”的setter。但PhpStorm似乎忽略了这一点,并显示了一条警告:
$bar = (new Bar())
->setAaa(new Aaa())
->setAaa(new Bbb())
;
在…\Foo中找不到方法“setbb”
在这种情况下,有没有办法让自动完成按预期工作?首先--修复代码示例--使其真实,而不是一些看起来像PHP的文本块
——扩展什么类栏扩展
- 什么是
方法setAaa()
- 什么是
方法?您的代码示例没有它setbb()
无论如何。。。至于实际的问题,在做了所有的更改之后,看起来像是真正的PHP代码 使用PHPDoc并确保它显示
@return$this
。现在它将:self
部分中的self
解释为特定类(即Foo
)。。。而setPropertyBbb()
显然在Foo
类中不可用。通过指定@return$this
可以使其在IDE中流畅
<?php
class Foo
{
private $aaa;
/**
* My super method
*
* @param Aaa $aaa
* @return $this
*/
public function setPropertyAaa(Aaa $aaa): self
{
$this->aaa = $aaa;
return $this;
}
}
class Bar extends Foo
{
private $bbb;
public function setPropertyBbb(Bbb $bbb): self
{
$this->bbb = $bbb;
return $this;
}
}
$bar = (new Bar())
->setPropertyAaa(new Aaa())
->setPropertyBbb(new Bbb())
;
谢谢您的回答!我已经更正了问题,谢谢你的评论!是的,@return$this
解决了这个问题。为setter找到一个没有PHPDoc的解决方案会很好,但是在这种情况下,您的权利,IDE将使用定义的返回类型,它是self
——并且self
是静态的。一般来说,我希望IDE在:self
中将self
视为$this
(意思是“当前或子类”). 但现在它只将其视为当前类(就像PHPDoc中处理@return self
)。我说不清为什么会这样做。。但这正是我们现在所拥有的。如果你认为这是一只虫子。。或者只是想要一个更好的默认解释——请随时在