How to define abstract fromArray method for enums in Php?

39 views Asked by At

I have following piece of code in several enums:

public static function fromArray(array $array): array
{
    return array_map(
        fn ($element) => self::from($element),
        $array
    );
}

Is there a way to be smarter about this and avoid code duplication?

As enums aren't extendable, I was thinking about approach with traits, but the issue is that trait would be dependent on the enum.

Any suggestions?

0

There are 0 answers