php 使用包含常量名称的简单变量访问类常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7506530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing a class constant using a simple variable which contains the name of the constant
提问by Adam Arold
I'm trying to access a class constant in one of my classes:
我正在尝试访问我的一个类中的类常量:
const MY_CONST = "value";
If I have a variable which holds the name of this constant like this:
如果我有一个变量,它像这样保存这个常量的名称:
$myVar = "MY_CONST";
Can I access the value of MY_CONST somehow?
我可以以某种方式访问 MY_CONST 的值吗?
self::$myVar
does not work obviously because it is for static properties. Variable variables does not work either.
显然不起作用,因为它用于静态属性。可变变量也不起作用。
回答by webbiedave
There are two ways to do this: using the constantfunction or using reflection.
Constant Function
常数函数
The constant function works with constants declared through define
as well as class constants:
常量函数与通过声明的常量define
以及类常量一起使用:
class A
{
const MY_CONST = 'myval';
static function test()
{
$c = 'MY_CONST';
return constant('self::'. $c);
}
}
echo A::test(); // output: myval
Reflection Class
反射类
A second, more laborious way, would be through reflection:
第二种更费力的方法是通过反思:
$ref = new ReflectionClass('A');
$constName = 'MY_CONST';
echo $ref->getConstant($constName); // output: myval
回答by mario
There is no syntax for that, but you can use an explicit lookup:
没有相应的语法,但您可以使用显式查找:
print constant("classname::$myConst");
I believe it also works with self::
.
我相信它也适用于self::
.
回答by hakre
Can I access the value of MY_CONST somehow?
我可以以某种方式访问 MY_CONST 的值吗?
self::MY_CONST
If you want to access is dynamically, you can use the reflection API Docs:
如果要动态访问,可以使用反射 API Docs:
$myvar = 'MY_CONST';
$class = new ReflectionClass(self);
$const = $class->getConstant($myVar);
The benefit with the reflection API can be that you can get all constants at once (getConstants
).
反射 API 的好处是您可以一次获取所有常量 ( getConstants
)。
If you dislike the reflection API because you don't wanna use it, an alternative is the constant
function (Demo):
如果您因为不想使用反射 API 而不喜欢它,那么另一种选择是constant
函数 ( Demo):
$myvar = 'MY_CONST';
class foo {const MY_CONST = 'bar';}
define('self', 'foo');
echo constant(self.'::'.$myvar);
回答by shawrie
have you tried
你有没有尝试过
$myVar = MY_CONST or $myVar = $MY_CONST
回答by Fabiano
Just a note for Reflection: the constructor for ReflectionClass must receive the full pathof the class for its parameter. This means that just setting the string 'A' as a constructor parameter may not work in some cases.
只是对 Reflection 的说明:ReflectionClass 的构造函数必须为其参数接收类的完整路径。这意味着在某些情况下,仅将字符串 'A' 设置为构造函数参数可能不起作用。
To avoid this problem, when using ReflectionClass you will be better if you do this:
为了避免这个问题,在使用 ReflectionClass 时,如果你这样做会更好:
$classA = new A();
$name_classA = get_class($classA);
$ref = new ReflectionClass(get_class($name_classA));
$constName = 'MY_CONST';
echo $ref->getConstant($constName);
Function get_class will give you the full path of a class whenever you are in the code. Missing the full path may result in a "Class not found" PHP error.
每当您在代码中时,函数 get_class 都会为您提供类的完整路径。缺少完整路径可能会导致“找不到类”PHP 错误。