我可以使用变量作为常量名称来访问PHP类常量吗? [英] Can I access a PHP Class Constant using a variable for the constant name?

查看:193
本文介绍了我可以使用变量作为常量名称来访问PHP类常量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

访问类常量时,我​​看到可以使用变量作为类名,例如$classname::CONST_VALUE.

When accessing a class constant I see that I can use a variable for the class name, e.g. $classname::CONST_VALUE.

如果我想为常量名称使用变量,例如self::$constant.这似乎不起作用.有解决方法吗?

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?

推荐答案

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

constant($variable);

查看文档: http://php.net/constant

这篇关于我可以使用变量作为常量名称来访问PHP类常量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆