0

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?

1

2 Answers 2

1

What about ::getConstants instead...?

Sign up to request clarification or add additional context in comments.

1 Comment

Talk about the answer being right in front of my face. Thanks.
0

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
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.