14

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?

adlawson
  • 6,303
  • 1
  • 35
  • 46
DatsunBing
  • 8,684
  • 17
  • 87
  • 172

1 Answers1

23
$variable = $classname.'::'.$constant;

constant($variable);

See the docs: http://php.net/constant

chiborg
  • 26,978
  • 14
  • 97
  • 115
Summoner
  • 1,397
  • 1
  • 9
  • 9