Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I access a PHP Class Constant using a variable for the constant name?

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?

like image 829
DatsunBing Avatar asked Jul 30 '12 08:07

DatsunBing


1 Answers

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

constant($variable);

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

like image 64
Summoner Avatar answered Oct 13 '22 11:10

Summoner