How can I get a list of static variables in a clas

2019-01-23 04:50发布

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?

2条回答
倾城 Initia
2楼-- · 2019-01-23 05:01

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

查看更多
混吃等死
3楼-- · 2019-01-23 05:08

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

$class = new ReflectionClass('MyClass');
$arr = $class->getStaticProperties();
Array
(
    [var1] => a
    [var2] => b
)
查看更多
登录 后发表回答