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?
A static member variable: • Belongs to the whole class, and there is only one of it, regardless of the number of objects. Must be defined and initialized outside of any function, like a global variable. It can be accessed by any member function of the class. Normally, it is accessed with the class scope operator.
Static variables can be accessed by calling with the class name ClassName. VariableName. When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.
Static variables and methods belong to a class and are called with the Class Name rather than using object variables, like ClassName. methodName(); There is only one copy of a static variable or method for the whole class.
From outside the class, "static variables should be accessed by calling with class name." From the inside, the class qualification is inferred by the compiler.
You can use ReflectionClass::getStaticProperties()
to do this:
$class = new ReflectionClass('MyClass');
$arr = $class->getStaticProperties();
Array ( [var1] => a [var2] => b )
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With