如何获取类中的静态变量列表?

发布于 2024-12-25 14:36:53 字数 241 浏览 1 评论 0原文

对于像...这样的类

class MyClass {
    static var1 = "a";
    static var2 = "b";
}

,我想在运行时检索静态成员及其值;类似于

Array(
    "var1" => "a",
    "var2" => "b"
)

PHP 有什么方法可以做到这一点吗?

With a class like

class MyClass {
    static var1 = "a";
    static var2 = "b";
}

... I'd like to retrieve the static members and their values at runtime; something like

Array(
    "var1" => "a",
    "var2" => "b"
)

Is there any way to do this in PHP?

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

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

发布评论

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

评论(2

池予 2025-01-01 14:36:53

您可以使用 ReflectionClass::getStaticProperties() 来执行此操作:

$class = new ReflectionClass('MyClass');
$arr = $class->getStaticProperties();
Array
(
    [var1] => a
    [var2] => b
)

You can use ReflectionClass::getStaticProperties() to do this:

$class = new ReflectionClass('MyClass');
$arr = $class->getStaticProperties();
Array
(
    [var1] => a
    [var2] => b
)
彼岸花ソ最美的依靠 2025-01-01 14:36:53

http://www.php.net/manual/en/reflectionclass.getstaticproperties.php - 尝试

获取有关类和类属性的信息(例如所有静态方法)称为“反射”。

http://www.php.net/manual/en/reflectionclass.getstaticproperties.php - try this

getting information about classes and class properties such as all static methods is called "reflection".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文