How to iterate over static data elements of a class - using a variable for the class name?

In PHP, if I have a class with static member variables, for example:

class Foo
{
    public static $a = 0;
    public static $b = 1;
}

... and I have a string variable containing the class name:

$foo = 'Foo';

... how do I iterate over the elements of a static class element Foousing a variable $foo?

Sort of:

// Does not work
foreach ($foo AS $field => &$value) {
    // Desired:
    // Iteration 1: $field = 'a', $value = 0
    // Iteration 2: $field = 'b', $value = 1
}

As noted, the code snippet above does not work.

Is it possible? If so, what is the syntax?

Thank.

+3
source share
1 answer
$class = new ReflectionClass('Foo');
$staticMembers = $class->getStaticProperties();

foreach($staticMembers as $field => &$value) {
+4
source

All Articles