How can I get a list of static variables in a class?

With class type

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

... I would like to get static members and their values ​​at runtime; sort of

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

Is there a way to do this in PHP?

+13
source share
2 answers

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

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

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

Getting information about classes and class properties, such as all static methods, is called reflection.

+2
source

Source: https://habr.com/ru/post/1667580/


All Articles