PHPackages                             myclabs/php-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. myclabs/php-enum

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

myclabs/php-enum
================

PHP Enum implementation

1.8.5(1y ago)2.7k227.9M—0.9%128[12 issues](https://github.com/myclabs/php-enum/issues)[3 PRs](https://github.com/myclabs/php-enum/pulls)20MITPHPPHP ^7.3 || ^8.0CI passing

Since Mar 19Pushed 1y ago29 watchersCompare

[ Source](https://github.com/myclabs/php-enum)[ Packagist](https://packagist.org/packages/myclabs/php-enum)[ Docs](https://github.com/myclabs/php-enum)[ GitHub Sponsors](https://github.com/mnapoli)[ Fund](https://tidelift.com/funding/github/packagist/myclabs/php-enum)[ RSS](/packages/myclabs-php-enum/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (36)Used By (20)

PHP Enum implementation inspired from SplEnum
=============================================

[](#php-enum-implementation-inspired-from-splenum)

[![GitHub Actions](https://github.com/myclabs/php-enum/workflows/CI/badge.svg)](https://github.com/myclabs/php-enum/actions?query=workflow%3A%22CI%22+branch%3Amaster)[![Latest Stable Version](https://camo.githubusercontent.com/5a5207ce7e89994f8e41c92417ff4f025155fb59b30ba921bb639170b3bcac3a/68747470733a2f2f706f7365722e707567782e6f72672f6d79636c6162732f7068702d656e756d2f76657273696f6e2e737667)](https://packagist.org/packages/myclabs/php-enum)[![Total Downloads](https://camo.githubusercontent.com/4cd89cb7d947e0f4489eee515d880d6a1371873d4c26d352b8c61947687a8811/68747470733a2f2f706f7365722e707567782e6f72672f6d79636c6162732f7068702d656e756d2f646f776e6c6f6164732e737667)](https://packagist.org/packages/myclabs/php-enum)[![Psalm Shepherd](https://camo.githubusercontent.com/7ba80d076ab2c46b3f99649ce011bf77c58fe58b4bfce34a032c3306b10bf9a2/68747470733a2f2f73686570686572642e6465762f6769746875622f6d79636c6162732f7068702d656e756d2f636f7665726167652e737667)](https://shepherd.dev/github/myclabs/php-enum)

Maintenance for this project is [supported via Tidelift](https://tidelift.com/subscription/pkg/packagist-myclabs-php-enum?utm_source=packagist-myclabs-php-enum&utm_medium=referral&utm_campaign=readme).

Why?
----

[](#why)

First, and mainly, `SplEnum` is not integrated to PHP, you have to install the extension separately.

Using an enum instead of class constants provides the following advantages:

- You can use an enum as a parameter type: `function setAction(Action $action) {`
- You can use an enum as a return type: `function getAction() : Action {`
- You can enrich the enum with methods (e.g. `format`, `parse`, …)
- You can extend the enum to add new values (make your enum `final` to prevent it)
- You can get a list of all the possible values (see below)

This Enum class is not intended to replace class constants, but only to be used when it makes sense.

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

[](#installation)

```
composer require myclabs/php-enum

```

Declaration
-----------

[](#declaration)

```
use MyCLabs\Enum\Enum;

/**
 * Action enum
 *
 * @extends Enum
 */
final class Action extends Enum
{
    private const VIEW = 'view';
    private const EDIT = 'edit';
}
```

Usage
-----

[](#usage)

```
$action = Action::VIEW();

// or with a dynamic key:
$action = Action::$key();
// or with a dynamic value:
$action = Action::from($value);
// or
$action = new Action($value);
```

As you can see, static methods are automatically implemented to provide quick access to an enum value.

One advantage over using class constants is to be able to use an enum as a parameter type:

```
function setAction(Action $action) {
    // ...
}
```

Documentation
-------------

[](#documentation)

- `__construct()` The constructor checks that the value exist in the enum
- `__toString()` You can `echo $myValue`, it will display the enum value (value of the constant)
- `getValue()` Returns the current value of the enum
- `getKey()` Returns the key of the current value on Enum
- `equals()` Tests whether enum instances are equal (returns `true` if enum values are equal, `false` otherwise)

Static methods:

- `from()` Creates an Enum instance, checking that the value exist in the enum
- `toArray()` method Returns all possible values as an array (constant name in key, constant value in value)
- `keys()` Returns the names (keys) of all constants in the Enum class
- `values()` Returns instances of the Enum class of all Enum constants (constant name in key, Enum instance in value)
- `isValid()` Check if tested value is valid on enum set
- `isValidKey()` Check if tested key is valid on enum set
- `assertValidValue()` Assert the value is valid on enum set, throwing exception otherwise
- `search()` Return key for searched value

### Static methods

[](#static-methods)

```
final class Action extends Enum
{
    private const VIEW = 'view';
    private const EDIT = 'edit';
}

// Static method:
$action = Action::VIEW();
$action = Action::EDIT();
```

Static method helpers are implemented using [`__callStatic()`](http://www.php.net/manual/en/language.oop5.overloading.php#object.callstatic).

If you care about IDE autocompletion, you can either implement the static methods yourself:

```
final class Action extends Enum
{
    private const VIEW = 'view';

    /**
     * @return Action
     */
    public static function VIEW() {
        return new Action(self::VIEW);
    }
}
```

or you can use phpdoc (this is supported in PhpStorm for example):

```
/**
 * @method static Action VIEW()
 * @method static Action EDIT()
 */
final class Action extends Enum
{
    private const VIEW = 'view';
    private const EDIT = 'edit';
}
```

Native enums and migration
--------------------------

[](#native-enums-and-migration)

Native enum arrived to PHP in version 8.1:
If your project is running PHP 8.1+ or your library has it as a minimum requirement you should use it instead of this library.

When migrating from `myclabs/php-enum`, the effort should be small if the usage was in the recommended way:

- private constants
- final classes
- no method overridden

Changes for migration:

- Class definition should be changed from

```
/**
 * @method static Action VIEW()
 * @method static Action EDIT()
 */
final class Action extends Enum
{
    private const VIEW = 'view';
    private const EDIT = 'edit';
}
```

to

```
enum Action: string
{
    case VIEW = 'view';
    case EDIT = 'edit';
}
```

All places where the class was used as a type will continue to work.

Usages and the change needed:

Operationmyclabs/php-enumnative enumObtain an instance will change from`$enumCase = Action::VIEW()``$enumCase = Action::VIEW`Create an enum from a backed value`$enumCase = new Action('view')``$enumCase = Action::from('view')`Get the backed value of the enum instance`$enumCase->getValue()``$enumCase->value`Compare two enum instances`$enumCase1 == $enumCase2`
 or
 `$enumCase1->equals($enumCase2)``$enumCase1 === $enumCase2`Get the key/name of the enum instance`$enumCase->getKey()``$enumCase->name`Get a list of all the possible instances of the enum`Action::values()``Action::cases()`Get a map of possible instances of the enum mapped by name`Action::values()``array_combine(array_map(fn($case) => $case->name, Action::cases()), Action::cases())`
 or
 `(new ReflectionEnum(Action::class))->getConstants()`Get a list of all possible names of the enum`Action::keys()``array_map(fn($case) => $case->name, Action::cases())`Get a list of all possible backed values of the enum`Action::toArray()``array_map(fn($case) => $case->value, Action::cases())`Get a map of possible backed values of the enum mapped by name`Action::toArray()``array_combine(array_map(fn($case) => $case->name, Action::cases()), array_map(fn($case) => $case->value, Action::cases()))`
 or
 `array_map(fn($case) => $case->value, (new ReflectionEnum(Action::class))->getConstants()))`Related projects
----------------

[](#related-projects)

- [PHP 8.1+ native enum](https://www.php.net/enumerations)
- [Doctrine enum mapping](https://github.com/acelaya/doctrine-enum-type)
- [Symfony ParamConverter integration](https://github.com/Ex3v/MyCLabsEnumParamConverter)
- [PHPStan integration](https://github.com/timeweb/phpstan-enum)

###  Health Score

67

—

FairBetter than 100% of packages

Maintenance42

Moderate activity, may be stable

Popularity81

Widely adopted with strong download metrics

Community57

Growing community involvement

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 51.6% 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 ~127 days

Recently: every ~324 days

Total

35

Last Release

489d ago

PHP version history (5 changes)1.3.0PHP &gt;=5.3

1.6.0PHP &gt;=5.4

1.7.0PHP &gt;=7.2

1.7.1PHP &gt;=7.1

1.8.0PHP ^7.3 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/329a6111724074f5388e95dd41a03ccf3c43f4bfe1ecf27c94c9efc6f7823228?d=identicon)[mnapoli](/maintainers/mnapoli)

![](https://www.gravatar.com/avatar/8220feb8a3d9f1df987987c33494da696aca6929179a6281cdbe45623fcbec0f?d=identicon)[myclabs](/maintainers/myclabs)

---

Top Contributors

[![mnapoli](https://avatars.githubusercontent.com/u/720328?v=4)](https://github.com/mnapoli "mnapoli (115 commits)")[![drealecs](https://avatars.githubusercontent.com/u/209984?v=4)](https://github.com/drealecs "drealecs (14 commits)")[![KartaviK](https://avatars.githubusercontent.com/u/5941637?v=4)](https://github.com/KartaviK "KartaviK (13 commits)")[![jarstelfox](https://avatars.githubusercontent.com/u/857362?v=4)](https://github.com/jarstelfox "jarstelfox (11 commits)")[![peter-gribanov](https://avatars.githubusercontent.com/u/1954436?v=4)](https://github.com/peter-gribanov "peter-gribanov (10 commits)")[![mirfilip](https://avatars.githubusercontent.com/u/1812341?v=4)](https://github.com/mirfilip "mirfilip (7 commits)")[![jeremykendall](https://avatars.githubusercontent.com/u/288613?v=4)](https://github.com/jeremykendall "jeremykendall (6 commits)")[![lorenzomar](https://avatars.githubusercontent.com/u/612386?v=4)](https://github.com/lorenzomar "lorenzomar (4 commits)")[![danielcosta](https://avatars.githubusercontent.com/u/42549?v=4)](https://github.com/danielcosta "danielcosta (4 commits)")[![simPod](https://avatars.githubusercontent.com/u/327717?v=4)](https://github.com/simPod "simPod (3 commits)")[![chriseskow](https://avatars.githubusercontent.com/u/19055?v=4)](https://github.com/chriseskow "chriseskow (3 commits)")[![danielbeardsley](https://avatars.githubusercontent.com/u/26855?v=4)](https://github.com/danielbeardsley "danielbeardsley (3 commits)")[![garoevans](https://avatars.githubusercontent.com/u/1016708?v=4)](https://github.com/garoevans "garoevans (3 commits)")[![remicollet](https://avatars.githubusercontent.com/u/270445?v=4)](https://github.com/remicollet "remicollet (3 commits)")[![rogervila](https://avatars.githubusercontent.com/u/6053012?v=4)](https://github.com/rogervila "rogervila (3 commits)")[![BernardoSilva](https://avatars.githubusercontent.com/u/1537510?v=4)](https://github.com/BernardoSilva "BernardoSilva (3 commits)")[![kamazee](https://avatars.githubusercontent.com/u/231518?v=4)](https://github.com/kamazee "kamazee (2 commits)")[![KKKas](https://avatars.githubusercontent.com/u/60153?v=4)](https://github.com/KKKas "KKKas (2 commits)")[![it-can](https://avatars.githubusercontent.com/u/644288?v=4)](https://github.com/it-can "it-can (2 commits)")[![grogy](https://avatars.githubusercontent.com/u/1322983?v=4)](https://github.com/grogy "grogy (2 commits)")

---

Tags

enumphpphp-enumenum

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[dasprid/enum

PHP 7.1 enum implementation

382146.0M11](/packages/dasprid-enum)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49644.8M97](/packages/marc-mabe-php-enum)[spatie/laravel-enum

Laravel Enum support

3655.4M31](/packages/spatie-laravel-enum)[consistence/consistence

Consistence - consistent approach and additions to PHP's functionality

1831.1M18](/packages/consistence-consistence)[cerbero/enum

Zero-dependencies package to supercharge enum functionalities.

359207.5k2](/packages/cerbero-enum)

PHPackages © 2026

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