ReflectionProperty::getDefaultValue() for a property without a default value is deprecated, use ReflectionProperty::hasDefaultValue() to check if the default value exists¶
Description¶
ReflectionProperty::getDefaultValue() returns null both when the property genuinely defaults to null and when the property has no default value at all (such as an uninitialized typed property), so the return value alone cannot tell the two cases apart. Calling it for a property that has no default value is now deprecated in favour of an explicit existence check.
Example¶
<?php
class X {
public int $prop;
}
$r = new ReflectionProperty(X::class, 'prop');
var_dump($r->getDefaultValue());
?>
Literal Examples¶
ReflectionProperty::getDefaultValue() for a property without a default value is deprecated, use ReflectionProperty::hasDefaultValue() to check if the default value exists
Solutions¶
Call
ReflectionProperty::hasDefaultValue()first, and only callgetDefaultValue()once a default value is confirmed to exist.
See Also¶
Changed Behavior¶
This error may appear following an evolution in behavior, in previous versions. See ReflectionProperty::getDefaultValue.