【问题标题】:PhpStorm doesn't recognize methods that are not in the interfacePhpStorm 无法识别不在接口中的方法
【发布时间】:2017-05-25 23:37:31
【问题描述】:
<?php

interface iFoo {
    public function print(): iFoo;
}

class Foo implements iFoo {
    public function print(): iFoo {
        return $this;
    }

    public function chain(): iFoo {
        return $this;
    }
}

$foo = new Foo();
$foo->print()
    ->chain() // Method 'chain' not found in iFoo
    ->print();

如何让 PhpStorm 识别链方法,即使它不在合同中?

【问题讨论】:

    标签: php methods interface phpstorm contract


    【解决方案1】:

    这是因为您告诉 PHPStorm,如果您的返回类型是 Foo,您将有一个没有类 chain() 的 iFoo 返回类型,我想这会起作用。

    【讨论】:

    • 不会是因为不符合约定,也不能在界面中设置Foo作为返回类型。
    【解决方案2】:

    print() 方法返回iFoo 实例:

    public function print(): iFoo {
    

    iFoo 不包含chain() 方法,这就是您看到“找不到方法”的原因。您可以将返回类型更改为Foo 或将chain() 方法添加到iFoo

    【讨论】:

      猜你喜欢
      • 2017-07-24
      • 1970-01-01
      • 2014-12-17
      • 2020-10-19
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      相关资源
      最近更新 更多