PHP 8.1 is almost getting released, including support for Enumerations. I was testing some of the enum functionality and couldn't find much documentation about it. Hence my question: how do I get all values of an enum?
CodePudding user response:
After some research I found the answer. You can use the static method: cases()
.
enum Status
{
case PAID;
case Cancelled;
}
Status::cases();
The cases method will return an array with an enum (UnitEnum
interface) for each value.
CodePudding user response:
In addition to UnitEnum::casses()
you can use ReflectionEnum
with this
$reflection = new ReflectionEnum(Status::class);
$reflection->getCases();
note that in both cases you will not be able to get the enum methods. but as long as the ReflectionEnum
is extending the ReflectionClass
so you can use the rest of ReflectionClass methods such as getMethods