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

发布于 2024-12-05 19:51:48 字数 265 浏览 1 评论 0原文

我正在尝试访问我的一个类中的类常量:

const MY_CONST = "value";

如果我有一个变量保存该常量的名称,如下所示:

$myVar = "MY_CONST";

我可以以某种方式访问​​ MY_CONST 的值吗?

self::$myVar

这显然不起作用,因为它适用于静态属性。 此外,变量变量也不起作用。

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?

self::$myVar

This is not working obviously because it is for static properties.
Also, Variable variables does not work either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无名指的心愿 2024-12-12 19:51:48

有两种方法可以做到这一点:使用 constant 函数或使用 反射

常量函数

常量函数与通过 define 声明的常量以及类常量一起使用:

class A
{
    const MY_CONST = 'myval';

    static function test()
    {
        $c = 'MY_CONST';
        return constant('self::'. $c);
    }
}

echo A::test(); // output: myval

反射类

第二种更费力的方法是通过反射:

$ref = new ReflectionClass('A');
$constName = 'MY_CONST';
echo $ref->getConstant($constName); // output: myval

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:

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
幸福%小乖 2024-12-12 19:51:48

没有相应的语法,但您可以使用显式查找:

print constant("classname::$myConst");

我相信它也适用于 self::

There is no syntax for that, but you can use an explicit lookup:

print constant("classname::$myConst");

I believe it also works with self::.

樱桃奶球 2024-12-12 19:51:48

我可以以某种方式访问​​ MY_CONST 的值吗?

self::MY_CONST

如果要动态访问,可以使用 reflection API Docs

$myvar = 'MY_CONST';
$class = new ReflectionClass(self);
$const = $class->getConstant($myVar);

反射 API 的好处是您可以一次获取所有常量 (getConstants)。

如果您因为不想使用反射 API 而不喜欢它,则另一种选择是 constant 函数 (演示):

$myvar = 'MY_CONST';    
class foo {const MY_CONST = 'bar';}    
define('self', 'foo');    
echo constant(self.'::'.$myvar);

Can I access the value of MY_CONST somehow?

self::MY_CONST

If you want to access is dynamically, you can use the reflection 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).

If you dislike the reflection API because you don't wanna use it, an alternative is the constant function (Demo):

$myvar = 'MY_CONST';    
class foo {const MY_CONST = 'bar';}    
define('self', 'foo');    
echo constant(self.'::'.$myvar);
初懵 2024-12-12 19:51:48

Reflection 需要注意的是:ReflectionClass 的构造函数必须接收类的完整路径作为其参数。
这意味着在某些情况下,仅将字符串“A”设置为构造函数参数可能不起作用。

为了避免这个问题,当使用 ReflectionClass 时,如果这样做会更好:

$classA = new A();
$name_classA = get_class($classA);
$ref = new ReflectionClass(get_class($name_classA));
$constName = 'MY_CONST';
echo $ref->getConstant($constName);

每当您在代码中时,函数 get_class 都会为您提供类的完整路径。缺少完整路径可能会导致“找不到类”PHP 错误。

Just a note for Reflection: the constructor for ReflectionClass must receive the full path of the class for its parameter.
This means that just setting the string 'A' as a constructor parameter may not work in some cases.

To avoid this problem, when using ReflectionClass you will be better if you do this:

$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.

萧瑟寒风 2024-12-12 19:51:48

你试过吗

$myVar = MY_CONST or $myVar = $MY_CONST

have you tried

$myVar = MY_CONST or $myVar = $MY_CONST
亣腦蒛氧 2024-12-12 19:51:48

IMO 这个解决方案比现有的解决方案稍好:

class C {
   public const WUT=5;
}
$v = "WUT";
var_dump(constant(C::class . "::$v"));
=> int(5)
  • 马里奥的解决方案很接近,但不适用于名称空间,而我的解决方案/does/适用于名称空间: https://3v4l.org/SUYbi
  • 对于 self 使用 constant(self::class . “::$v”);

IMO this solution is slightly better than the existing ones:

class C {
   public const WUT=5;
}
$v = "WUT";
var_dump(constant(C::class . "::$v"));
=> int(5)
  • mario's solution comes close, but doesn't work with namespaces, while my solution /does/ work with namespaces: https://3v4l.org/SUYbi
  • for self use constant(self::class . "::$v");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文