【问题标题】:PHP type hinting in PHPDoc in derived classes派生类中 PHPDoc 中的 PHP 类型提示
【发布时间】:2015-12-30 20:41:46
【问题描述】:

看看这个代码示例:

class basetype {
    public function method() {
        return false;
    }
}

class extendtype extends basetype {
    public function methodb() {
        return true;
    }
}

class aa {
    /**
     * @var basetype
     */
    protected $membera;
}

class bb extends aa {
    public function __constructor() {
        $this->membera = new extendtype();
    }

    public function dosomething() {
        $this->membera->methodb();
    }
}

在 PHPStorm 中编辑时,我收到“在类基类型中找不到方法方法 b”的警告。我使用预先存在的代码库,无法更改基类。那么我该怎么做才能消除这个警告呢?

【问题讨论】:

  • 你为什么是@var basetype $membera 变量?是的,$membera 是一种基本类型,但这可能会让 PHPStrorm 感到困惑。只需删除它,看看你是否得到同样的错误。
  • 我认为您误读了保证。过去它形成phpstorm。归根结底,这只是一个提示,如果您的代码有效,则无关紧要。

标签: php phpstorm phpdoc type-hinting


【解决方案1】:

您可以在您的class BB 中覆盖$membera,并为其提供一个具有派生类型的新文档块。

class bb extends aa {
    /**
     * @var extendtype
     */
    protected $membera;

    public function __constructor() {
        $this->membera = new extendtype();
    }

    public function dosomething() {
        $this->membera->methodb();
    }
}

【讨论】:

    猜你喜欢
    • 2016-08-30
    • 2014-08-29
    • 2010-10-21
    • 2015-02-01
    • 2018-01-20
    • 1970-01-01
    • 2011-10-28
    • 2013-02-17
    相关资源
    最近更新 更多