脚本宝典收集整理的这篇文章主要介绍了PHPStorm代码提示对象数组的数组,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
class FooList { public function __construct(){ $this->_fooList[] = new Foo(1); $this->_fooList[] = new Foo(2); $this->_fooList[] = new Foo(3); $this->_fooList[] = new Foo(4); } /** * @return Foo[] */ getFoos() { return $this->_fooList; } }
所以,如果我这样做……
$fooList = new FooList(); foreach($fooList as $foo) { // Nice hinting. $foo->FooMethod... }
PHPStorm理解$fooList是一个Foos数组,因此知道$foo的类型是Foo.
问题是我想要一个FooList数组.
$listOfLists[] = new FooList(); $listOfLists[] = new FooList(); $listOfLists[] = new FooList(); $listOfLists[] = new FooList(); foreach ($listOfLists as $fooList) { foreach($fooList as $foo) { // No code hinting for $foo :( } }
我知道你可以在foreach中手动编写提示,比如……
foreach ($listOfLists as $fooList) { foreach($fooList as $foo) { /** $VAR $foo Foo */ // Code hinting,yay!! } }
要么 …
foreach ($listOfLists as $fooList) { /** $var $fooList Foo[] */ foreach($fooList as $foo) { // Code hinting,yay!! } }
但我觉得这很难看,因为$listOfLists是Foo数组的构建,它应该知道我在说什么,而不是每次实现listOfLists时都不提醒它.
有没有办法实现这个?
这意味着您现在可以执行此操作:
/** * @var $listOfLists Foo[][] */ $listOfLists[] = (new FooList())->getFoos(); $listOfLists[] = (new FooList())->getFoos(); $listOfLists[] = (new FooList())->getFoos(); $listOfLists[] = (new FooList())->getFoos(); foreach ($listOfLists as $fooList) { foreach($fooList as $foo) { // Code hinting,yay!! $foo->fooMethod(); } }
得到预期的:
以上是脚本宝典为你收集整理的PHPStorm代码提示对象数组的数组全部内容,希望文章能够帮你解决PHPStorm代码提示对象数组的数组所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。