Is A Class Constant Set?

../_images/isset_class_constant.png

When using a dynamic class constant, it is important to check if the constant is actually defined.

It is not possible to use the coalesce ?? operator, as a non-existent class constant yields a fatal error.

Then, it is not possible to use isset(), because a dynamic class constant is actually an expression. The error message is actually misleading, as the offered solution is not available.

The only solution is to use the defined() method, which is made to check constants. In the case of a class constant, one need to use the string syntax, and should not use the class constant syntax.

See Also

PHP Error Messages

PHP Features