How can I check if a constant is defined in a PHP class?
class Foo {
const BAR = 1;
}
Is there something like property_exists()
or method_exists()
for class constants? Or can I just use defined("Foo::BAR")
?
How can I check if a constant is defined in a PHP class?
class Foo {
const BAR = 1;
}
Is there something like property_exists()
or method_exists()
for class constants? Or can I just use defined("Foo::BAR")
?
You can use that function:
Or alternative version using ReflectionClass
You have 3 ways to do it:
defined()
[PHP >= 4 - most retro-compatible way]
ReflectionClass
[PHP >= 5]
ReflectionClassConstant
[PHP >= 7.1.0]
There is no real better way. Depends on your needs and use case.
You can check if a constant is defined with the code below:
Yes, just use the class name in front of the constant name.
http://www.php.net/manual/en/function.defined.php#106287