获取PHP中动态选择的类常量的值 [英] Get value of dynamically chosen class constant in PHP
本文介绍了获取PHP中动态选择的类常量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望能够做这样的事情:
class ThingIDs
{
const Something = 1;
const AnotherThing = 2;
}
$thing = 'Something';
$id = ThingIDs::$thing;
这不起作用.有一种简单的方法可以做一些等效的事情吗?请注意,我被班级困住了.它在我无法重写的库中.我正在编写在命令行上接受参数的代码,并且真的希望它接受符号名而不是ID号.
解决方案
$id = constant("ThingIDs::$thing");
http://php.net/manual/en/function.constant.php >
I would like to be able to do something like this:
class ThingIDs
{
const Something = 1;
const AnotherThing = 2;
}
$thing = 'Something';
$id = ThingIDs::$thing;
This doesn't work. Is there a straightforward way of doing something equivalent? Note that I'm stuck with the class; it's in a library I can't rewrite. I'm writing code that takes arguments on the command line, and I would really like it to take symbolic names instead of id numbers.
解决方案
$id = constant("ThingIDs::$thing");
http://php.net/manual/en/function.constant.php
这篇关于获取PHP中动态选择的类常量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文