ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists

Description

ReflectionClass::getConstant() returns false both when the requested constant does not exist and when the constant’s actual value is false, so the return value alone cannot tell the two cases apart. Calling it for a constant that does not exist is now deprecated in favour of an explicit existence check.

Example

<?php

class X {
    const FOO = 1;
}

$class = new ReflectionClass(X::class);
$value = $class->getConstant('BAR');
var_dump($value);

?>

Literal Examples

  • ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists

Solutions

  • Call ReflectionClass::hasConstant() first, and only call getConstant() once existence is confirmed.

See Also

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ReflectionClass::getConstant.