Skip to content

utilitte/enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Type safe enums

Getting started

Create enum class

/**
 * @method static self ENGLAND() 
 * @method static self USA() 
 */
class CountryEnum extends Enum
{
    protected static function getEnums(): array
    {
        return ['england', 'usa'];
    }

}

Usage

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());