This package offers strongly typed enums for PHP with some nice distinctive features.
Quick functionality example:
usePalshin\Enum\Enum;
/** * @method static self CLIENT() = 1 * @method static self ADMIN() * @method static self MANAGER()*/finalclass UserRole extends Enum
{
}
UserRole::MANAGER() instanceof UserRole; // true
UserRole::MANAGER() === UserRole::MANAGER(); // true
UserRole::MANAGER()->value; // 3
UserRole::all(); // [ UserRole::CLIENT(), UserRole::ADMIN(), UserRole::MANAGER() ]
UserRole::toArray(); // [ 'CLIENT' => 1, 'ADMIN' => 2, 'MANAGER' => 3 ]
UserRole::toValues(); // [1, 2, 3]
UserRole::toNames(); // ['CLIENT', 'ADMIN', 'MANAGER']
UserRole::CLIENT()->id(); // FQCN with enum member name: App\Enums\UserRole::CLIENTecho UserRole::CLIENT(); // print "1"echojson_encode(['enumMember' => UserRole::CLIENT()]); // print "{enumMember:1}"Install package using composer:
composer require epalshin/enumYou can choose one of three ways to define enumeration members (or its combination).
PHPDoc comment:
usePalshin\Enum\Enum;
/** * @method static self CLIENT() = 1 * @method static self ADMIN() * @method static self MANAGER()*/finalclass UserRole extends Enum
{
}Class constants:
usePalshin\Enum\Enum;
finalclass UserRole extends Enum
{
privateconstCLIENT = 1;
privateconstADMIN = 2;
privateconstMANAGER = 3;
}Static method:
usePalshin\Enum\Enum;
finalclass UserRole extends Enum
{
publicstaticfunctionvalues(): array
{
return [
'CLIENT' => 1,
'ADMIN' => 2,
'MANAGER' => 3,
]; }
}Also you can combine few (or even all) methods in your declaration:
usePalshin\Enum\Enum;
/** * @method static self CLIENT()*/finalclass UserRole extends Enum
{
privateconstADMIN = 2;
publicstaticfunctionvalues(): array
{
return [
'MANAGER' => 3,
]; }
}❗ If you decided to combine ways to declare enumeration members, be careful with members intersection: name of enum member is unique for enum class so different values of same member can lead to unobvious errors in your code. The priority of names is: constant declaration, functional declaration and PHPDoc commentary declaration. Names are case sensitive.
Personally I prefer the way with PHPDoc comment but I think if you want keep access to enumeration values without class wrapping and get the IDE's autocompletion benefits you can do something like this:
usePalshin\Enum\Enum;
/** * @method static self CLIENT() * @method static self ADMIN() * @method static self MANAGER()*/finalclass UserRole extends Enum
{
constCLIENT = 1;
constADMIN = 2;
constMANAGER = 3;
}
UserRole::CLIENT === 1; // true
UserRole::CLIENT() instanceof UserRole; // trueIn some cases may be useful to add meta information to your enumeration class. You can do it like this:
usePalshin\Enum\Enum;
/** * @method static self CLIENT() * @method static self MANAGER() * @method static self ADMIN()*/class UserRole extends Enum
{
publicstaticfunctionlabels(): array
{
return [
'CLIENT' => 'Client account',
'MANAGER' => 'Manager account',
'ADMIN' => 'Administrator account'
];
}
publicstaticfunctiondescriptions(): array
{
return [
'CLIENT' => 'Online store buyer',
'MANAGER' => 'The person who is responsible for interaction with the client',
'ADMIN' => 'The person who controls the managers'
];
}
}
UserRole::CLIENT()->label; // Client account
UserRole::MANAGER()->description; // The person who is responsible for interaction with the client