如何获取一个类中的静态变量列表? [英] How can I get a list of static variables in a class?
本文介绍了如何获取一个类中的静态变量列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用类
class MyClass {
static var1 =a;
static var2 =b;
}
...我想检索静态成员及其值运行;类似
Array(
var1=>a,
var2= >b
)
有什么方法可以在PHP中执行此操作? / p>
解决方案
您可以使用 ReflectionClass :: getStaticProperties()
:
$ class = new ReflectionClass('MyClass');
$ arr = $ class-> getStaticProperties();
数组
(
[var1] = > a
[var2] => b
)
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?
解决方案You can use
ReflectionClass::getStaticProperties()
to do this:$class = new ReflectionClass('MyClass'); $arr = $class->getStaticProperties();
Array ( [var1] => a [var2] => b )
这篇关于如何获取一个类中的静态变量列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文