[英]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, eg $classname::CONST_VALUE
. 当访问类常量时,我看到我可以使用变量作为类名,例如$classname::CONST_VALUE
。
What if I want to use a variable for the constant name, eg self::$constant
. 如果我想使用变量作为常量名称,例如self::$constant
,该怎么办? This does not seem to work. 这似乎不起作用。 Is there a workaround? 有解决方法吗?
$variable = $classname.'::'.$constant;
constant($variable);
See the docs: http://php.net/constant 请参阅文档: http : //php.net/constant
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.