Matt
Matt

Reputation: 1573

"enumeration" class, get all values

I have a class I've written to act like an enumeration:

abstract class Enum {
   const VAL1 = "Val1";
   const VAL2 = "Val2";
}

I want to define a static function that will return all the constant members of the class but the only function I can find that does that is ReflectionClass::getStaticProperties. Unfortunately it doesn't look like I can use it this way. My understanding is that constants are implicitly static but the function is ignoring them. Is there a method I haven't found that will give me an array of constants in a similar way?

Upvotes: 0

Views: 101

Answers (2)

jpiasetz
jpiasetz

Reputation: 1772

You can use Reflection for this.

<?php
class Enum {
    const VAL1 = "Val1";
    const VAL2 = "Val2";
}


$refl = new ReflectionClass('Enum');
print_r($refl->getConstants());

Output:

Array
(
    ['VAL1'] => Val1
    ['VAL2'] => Val2
)

Upvotes: 0

deceze
deceze

Reputation: 522523

What about ::getConstants instead...?

Upvotes: 1

Related Questions