在我的 PHP 应用程序中,我有一个用于所有数据库对象的基类,它由特定的模型类进一步扩展。它是这样进行的(简化):
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 无法确定对象具有哪些方法,并突出显示我作为错误使用的方法。因此,没有可用的类型提示。
例如,假设那个final class User extends Model
类User
有方法sendNotification
,这种代码:
User::getById($id) -> sendNotification($param1, $param2, ...)
已经sendNotification
泛黄了,就好像它不存在一样。
我知道这不是什么大问题,但是这真的很烦人,因为我被不断的冗余警告分心,而且在这种用例中我不能使用类型提示。