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

非 Y 不嫁゛ 提交于 2019-12-05 13:03:35

问题


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?


回答1:


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

constant($variable);

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



来源:https://stackoverflow.com/questions/11717593/can-i-access-a-php-class-constant-using-a-variable-for-the-constant-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!