Can I access a PHP Class Constant using a variable for the constant name?
-
23-06-2021 - |
Question
When accessing a class constant I see that I can use a variable for the class name, e.g. $classname::CONST_VALUE
.
What if I want to use a variable for the constant name, e.g. self::$constant
. This does not seem to work. Is there a workaround?
La solution
$variable = $classname.'::'.$constant;
constant($variable);
See the docs: http://php.net/constant
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow