【问题标题】:In Php, how do I set the return type of a variable using inline type hinting在 PHP 中,如何使用内联类型提示设置变量的返回类型
【发布时间】:2020-06-05 00:44:49
【问题描述】:

我怎样才能避免这个“错误”:

DataObject::get_one 的基本文档块说它返回一个DataObject,这是真的,但大多数时候它是一个扩展 DataObject 的类 - 例如class HealthCheck extends DataObject 在这种情况下。

如果我写:

        /** @var HealthCheck|null */
        $a = DataObject::get_one(HealthCheck::class, ['Published' => true]);
        return $a;

然后 scrutiziner 不会给我一个错误,但我确实想要 / 并且不能这样写。

【问题讨论】:

    标签: silverstripe type-hinting docblocks scrutinizer


    【解决方案1】:

    我们可以做到以下几点:

    return HealthCheck::get()->filter(['Published' => true])->first();
    

    【讨论】:

      猜你喜欢
      • 2010-09-07
      • 2016-08-01
      • 2019-03-24
      • 1970-01-01
      • 2010-12-20
      • 2015-11-23
      • 2016-02-29
      • 2016-12-23
      • 2021-12-08
      相关资源
      最近更新 更多