I’m trying to access a class constant in one of my classes:
const MY_CONST = "value";
If I have a variable which holds the name of this constant like this:
$myVar = "MY_CONST";
Can I access the value of MY_CONST somehow?
self::$myVar
This is not working obviously because it is for static properties.
Also, Variable variables does not work either.
There are two ways to do this: using the constant function or using reflection.
Constant Function
The constant function works with constants declared through
defineas well as class constants:Reflection Class
A second, more laborious way, would be through reflection: