Reputation: 1573
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
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