class MyClass { const MY_CONSTANT = 123; } $refClass = new ReflectionClass('MyClass'); if ($refClass->hasConstant('MY_CONSTANT')) { echo "MyClass has MY_CONSTANT"; } else { echo "MyClass does not have MY_CONSTANT"; }
interface MyInterface { const MY_CONSTANT = 'Hello World'; } $refClass = new ReflectionClass('MyInterface'); if ($refClass->hasConstant('MY_CONSTANT')) { echo "MyInterface has MY_CONSTANT"; } else { echo "MyInterface does not have MY_CONSTANT"; }Output: MyInterface has MY_CONSTANT This function is a part of the PHP Reflection package which provides an API for introspecting classes, interfaces, functions, methods, and extensions.