I want to refactor my codebase from MyCLabs Enums to PHP’s native enum’s. I’m already running into a few small issues:
- How can I typehint that an argument or return type must be an enum?
- How can I check if a value is an enum?
The best I can do with regards to typehinting is typehinting object
.
The best I can do for checking if a value is an enum is with with a ReflectionClass
, eg:
if (
is_object($value)
&& (new ReflectionClass($value))->isEnum()
) {
// it's an enum.
}
Without these simple checks, it feels like refactoring to native enums is a step backwards. Am I approaching it incorrectly?