PhpStorm无法在父抽象类通过后期静态绑定创建的对象中找到方法 [英] PhpStorm not finding methods in objects created by late static binding by parent abstract class

查看:202
本文介绍了PhpStorm无法在父抽象类通过后期静态绑定创建的对象中找到方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的PHP应用程序中,我具有所有数据库对象的基类,特定的模型类进一步扩展了该基类.它是这样(简化)的:

In my PHP application I have a base class for all database objects, which is further extended by specific model classes. It goes on like this (simplified):

abstract class Model extends Collection {

    (...)

    function __construct(string $primary_key, string $value) {
        $data = MysqlDB::instance() -> select(static::TABLE, implode(',', static::COLUMNS), "$primary_key=\"$value\"");
        if (count($data) != 1)
            throw new ModelException('Select condition uncertain');
        parent::__construct($data[0]);
    }

    public static function getById(string $id) : ?Model {
        try {
            return new static('id', $id);
        } catch (ModelException $e) {
            return NULL;
        }
    }

    (...)

}

重点是,我在子类上使用getById函数来获取所需的对象.但是,由于getById方法的返回类型为Model,因此PhpStorm无法确定对象具有哪些方法,并突出显示了我用作错误的方法.因此,没有可用的类型提示.

The point is, I use the getById function on child classes to obtain the needed objects. However, since the return type of getById method is Model, PhpStorm can't figure out what methods the objects has and highlights the ones I use as an error. Consequently, no type-hinting available.

例如,假设final class User extends Model和类User具有方法sendNotification,则这种代码:

For instance, assuming that final class User extends Model and that class User has method sendNotification, this kind of code:

User::getById($id) -> sendNotification($param1, $param2, ...)

sendNotification泛黄,好像它不存在.

has sendNotification yellowed out as though it did not exist.

我知道这不是什么大问题,但是从不断的冗余警告中分散我的注意力,并且在这种使用情况下我不能使用类型提示,这确实令人不快.

I know this isn't that much of an issue, but it's really irritating in terms of that I get distracted by constant redundant warnings and that I can't use type-hinting in such a usage case.

推荐答案

在指定动态类型(例如@return static@return $this)的getById()上尝试使用实际的PHPDoc.

Try with actual PHPDoc for your getById() where you specify dynamic type (e.g. @return static or @return $this).

这是提供此类此类及其子级"提示的最常用方法.

That's the most common way of providing such "this class and its' children" typehint.

另一个可能的选择是相同的..,但是使用PHP 7.1功能(因此没有PHPDoc块).

Another possible option is kind of the same .. but using PHP 7.1 functionality (so no PHPDoc blocks).

尝试使用self作为返回类型而不是Model.我的意思是这里:

Try using self as return type instead of Model. I mean here:

public static function getById(string $id) : ?Model {

改为使用此:

public static function getById(string $id) : ?self {

但是对于此版本,您应该使用PhpStorm 2017.2 EAP构建,因为2017.1.x对此存在问题(请参见 https://stackoverflow.com/a/44806801/783119 作为示例).

But for this one you should use PhpStorm 2017.2 EAP build as 2017.1.x has issues with that (see https://stackoverflow.com/a/44806801/783119 as an example).

这篇关于PhpStorm无法在父抽象类通过后期静态绑定创建的对象中找到方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆