3

In PHP, if I have a class with static member variables, such as:

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

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

$foo = 'Foo';

... how do I loop over the static data members of class Foo using the variable $foo?

Something like:

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

As noted, the above code snippet does not work.

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

Thanks.

kpmDev
  • 1,330
  • 1
  • 10
  • 28
Dan Nissenbaum
  • 13,558
  • 21
  • 105
  • 181

1 Answers1

4
$class = new ReflectionClass('Foo');
$staticMembers = $class->getStaticProperties();

foreach($staticMembers as $field => &$value) {
zavg
  • 10,351
  • 4
  • 44
  • 67