In Php, how do I set the return type of a variable using inline type hinting

236 Views Asked by At

How can I avoid this "error":

showing error

Basically docblocks for DataObject::get_one says that it returns a DataObject, which is true, but most of the time it is a class that extends DataObject - e.g. class HealthCheck extends DataObject in this case.

If I write:

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

then scrutiziner does not give me an error but I do want / and can not to write it like this.

1

There are 1 best solutions below

0
On

We can do the following:

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