【问题标题】:PHPStorm not recognising more than 2 chained public methodsPHPStorm 无法识别超过 2 个链式公共方法
【发布时间】:2015-09-24 01:28:22
【问题描述】:

这不会影响代码,但有点烦人。

我的控制器中有这 3 个方法:

public function chainOne()
{
    return $this;
}

public function chainTwo()
{
    return $this;
}

public function chainThree()
{
    return $this;
}

到达特定路线后被调用的方法是这样的:

public function indexAction()
{

    $this->chainOne()
            ->chainTwo()
            ->chainThree();

}

PHPStorm 说method chainThree() not found in class $this。但是chainThree() 中的代码可以毫无问题地执行。

我该如何解决?是bug吗?

【问题讨论】:

标签: php ide phpstorm


【解决方案1】:

您可以使用 docblocks 帮助 PHPStorm 识别返回值:

public class Foo
{

    /**
    * @return Foo $this
    */
    public function chainOne()
    {
        return $this;
    }

    /**
    * @return Foo $this
    */
    public function chainTwo()
    {
        return $this;
    }

    /**
    * @return Foo $this
    */
    public function chainThree()
    {
        return $this;
    }

}

【讨论】:

  • @SuchMuchCode 如果你在子类中使用它会发生什么?它不会将$this 解析为当前类,而是留在父类Foo...
  • 您甚至不需要添加Foo,只需@return $this 就足够了,并且可以让您的代码“防重命名”。
猜你喜欢
  • 2017-07-24
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-16
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
相关资源
最近更新 更多