php - 获取PHP中动态选择的类常量的值

标签 php class-constants

我希望能够做这样的事情:

class ThingIDs
{
    const Something = 1;
    const AnotherThing = 2;
}

$thing = 'Something';
$id = ThingIDs::$thing;

这不起作用。有没有一种直接的方法来做等效的事情?请注意,我坚持上课;它在我无法重写的库中。我正在编写在命令行上接受参数的代码,我真的希望它采用符号名称而不是 id 数字。

最佳答案

使用 constant()功能:

$id = constant("ThingIDs::$thing");

关于php - 获取PHP中动态选择的类常量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6147102/

相关文章:

php - 删除 URL 中的旧参数 (PHP)

php - 所得税计算的编程逻辑

php - 如何将另一个文件中的变量分配给类常量?

compiler-errors - Eiffel : once function has generic or anchored result. Trying to create constants

php - 使用 zend 实现搜索

email - PHP邮件函数 'from'地址

php - 如何使用 SimpleXml 访问元素属性?

c# - 访问嵌套的静态常量

Java 使用 HashMap 和 switch 语句

c++ - 常量类成员、赋值运算符和 QList