samedi 2 octobre 2021

How to detect null default value for non-typed class property via reflection in PHP?

When using reflection (ReflectionClass), is it possible to detect, whether a non-typed class property has a null default value specified, or has no default value at all?

class A {
    public $foo;
    public $bar = null;
}

Here, for both foo and bar, the ReflectionProperty::getDefaultValue() returns null and ReflectionProperty::hasDefaultValue() returns true.

$rc = new ReflectionClass(A::class);
var_dump($rc->getProperty("foo")->hasDefaultValue()); // true
var_dump($rc->getProperty("foo")->getDefaultValue()); // null
var_dump($rc->getProperty("bar")->hasDefaultValue()); // true
var_dump($rc->getProperty("bar")->getDefaultValue()); // null

How can I differentiate between the two properties?





Aucun commentaire:

Enregistrer un commentaire