Skip to content
Advertisement

Generic enum type in php

How can I specify an argument type to take any enum value?

Something like function processEnum(enum $value) would be ideal, however nothing seems to exist?

JavaScript

Additionally it would be nice to separate backed vs non-backed enums or additionally backed types; enums that are backed as strings for example.

Advertisement

Answer

All enums implement the UnitEnum interface, and backed enums (those with a type and a ->value property) also implement the BackedEnum interface. You can write type constraints for those:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement