简体   繁体   English

使用包含常量名称的简单变量访问类常量

[英]Accessing a class constant using a simple variable which contains the name of the constant

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.可变变量也不起作用。

There are two ways to do this: using the constant function 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

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::

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);

你有没有尝试过

$myVar = MY_CONST or $myVar = $MY_CONST

Just a note for Reflection: the constructor for ReflectionClass must receive the full path of the class for its parameter.只是对 Reflection 的说明:ReflectionClass 的构造函数必须为其参数接收类的完整路径 This means that just setting the string 'A' as a constructor parameter may not work in some cases.这意味着在某些情况下,仅将字符串 '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.每当您在代码中时,函数 get_class 都会为您提供类的完整路径。 Missing the full path may result in a "Class not found" PHP error.缺少完整路径可能会导致“找不到类”PHP 错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM