加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHPStorm代码提示对象数组的数组

发布时间:2020-12-13 13:22:59 所属栏目:PHP教程 来源:网络整理
导读:在 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 Fo
在 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时都不提醒它.

有没有办法实现这个?

根据 comments by @LazyOne中链接的 bug report,截至 PhpStorm EAP 138.256(因此在PHPStorm 8中),现在支持统一的多级数组doc解析.

这意味着您现在可以执行此操作:

/**
 * @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();
    }
}

得到预期的:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读