Type safe enums
Create enum class
/**
* @method static self ENGLAND()
* @method static self USA()
*/
class CountryEnum extends Enum
{
protected static function getEnums(): array
{
return ['england', 'usa'];
}
}
static method must be always uppercase
assert(CountryEnum::USA() === CountryEnum::USA());
function country(CountryEnum $country): string {
return $country->value();
}
assert(country(CountryEnum::USA()) === 'usa');
convert enum value to object:
assert(CountryEnum::get('usa') === CountryEnum::USA());