PHPackages                             epalshin/enum - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. epalshin/enum

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

epalshin/enum
=============

Powerful enum library for PHP

2.0.0(5y ago)46.0k[1 PRs](https://github.com/palshin/enum/pulls)MITPHPPHP ^8.0

Since Nov 9Pushed 1y ago1 watchersCompare

[ Source](https://github.com/palshin/enum)[ Packagist](https://packagist.org/packages/epalshin/enum)[ RSS](/packages/epalshin-enum/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (4)Versions (4)Used By (0)

Enums for PHP
=============

[](#enums-for-php)

This package offers strongly typed enums for PHP with some nice distinctive features.

Quick functionality example:

```
use Palshin\Enum\Enum;

/**
 * @method static self CLIENT() = 1
 * @method static self ADMIN()
 * @method static self MANAGER()
*/
final class 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::CLIENT
echo UserRole::CLIENT(); // print "1"
echo json_encode(['enumMember' => UserRole::CLIENT()]); // print "{enumMember:1}"
```

Installation
------------

[](#installation)

Install package using composer:

```
composer require epalshin/enum
```

Usage
-----

[](#usage)

You can choose one of three ways to define enumeration members (or its combination).

PHPDoc comment:

```
use Palshin\Enum\Enum;

/**
 * @method static self CLIENT() = 1
 * @method static self ADMIN()
 * @method static self MANAGER()
*/
final class UserRole extends Enum
{
}
```

Class constants:

```
use Palshin\Enum\Enum;

final class UserRole extends Enum
{
  private const CLIENT = 1;
  private const ADMIN = 2;
  private const MANAGER = 3;
}
```

Static method:

```
use Palshin\Enum\Enum;

final class UserRole extends Enum
{
  public static function values(): array
  {
    return [
      'CLIENT' => 1,
      'ADMIN' => 2,
      'MANAGER' => 3,
    ];
  }
}
```

Also you can combine few (or even all) methods in your declaration:

```
use Palshin\Enum\Enum;

/**
 * @method static self CLIENT()
*/
final class UserRole extends Enum
{
  private const ADMIN = 2;

  public static function values(): 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:

```
use Palshin\Enum\Enum;

/**
 * @method static self CLIENT()
 * @method static self ADMIN()
 * @method static self MANAGER()
*/
final class UserRole extends Enum
{
  const CLIENT = 1;
  const ADMIN = 2;
  const MANAGER = 3;
}

UserRole::CLIENT === 1; // true
UserRole::CLIENT() instanceof UserRole; // true
```

In some cases may be useful to add meta information to your enumeration class. You can do it like this:

```
use Palshin\Enum\Enum;

/**
 * @method static self CLIENT()
 * @method static self MANAGER()
 * @method static self ADMIN()
*/
class UserRole extends Enum
{
  public static function labels(): array
  {
    return [
      'CLIENT' => 'Client account',
      'MANAGER' => 'Manager account',
      'ADMIN' => 'Administrator account'
    ];
  }

  public static function descriptions(): 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
```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~208 days

Total

2

Last Release

1853d ago

Major Versions

1.0.0 → 2.0.02021-06-06

PHP version history (2 changes)1.0.0PHP ^7.4

2.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/b8a21b17dfa739c39cf7c54dcae47926976e715ab70fa9ed41255780a3934385?d=identicon)[palshin](/maintainers/palshin)

---

Top Contributors

[![palshin](https://avatars.githubusercontent.com/u/28474694?v=4)](https://github.com/palshin "palshin (20 commits)")

---

Tags

enumphp

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/epalshin-enum/health.svg)

```
[![Health](https://phpackages.com/badges/epalshin-enum/health.svg)](https://phpackages.com/packages/epalshin-enum)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
