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

Abandoned → [myclabs/php-enum](/?search=myclabs%2Fphp-enum)Library[Utility &amp; Helpers](/categories/utility)

schleuse/php-enum
=================

PHP Enum implementation

1.8(3y ago)11.4kMITPHPPHP ^7.3 || ^8.0

Since Sep 16Pushed 3y agoCompare

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

READMEChangelogDependencies (3)Versions (3)Used By (0)

Fork of [myclabs/php-enum](https://github.com/myclabs/php-enum)
===============================================================

[](#fork-of-myclabsphp-enum)

Backports some fixes for PHP 8.1 compability while also mantaining compability with at least PHP 7.1

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

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

[![Build Status](https://camo.githubusercontent.com/63aeca38ad5928aab3b805d20df4449749292be260a6c044c2793b2f22436024/68747470733a2f2f7472617669732d63692e6f72672f6d79636c6162732f7068702d656e756d2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/myclabs/php-enum)[![Latest Stable Version](https://camo.githubusercontent.com/d6e65056a527da8f27d71c942ef9835420e3305c19bf0e0a5e499ebed8e77203/68747470733a2f2f706f7365722e707567782e6f72672f6d79636c6162732f7068702d656e756d2f76657273696f6e2e706e67)](https://packagist.org/packages/myclabs/php-enum)[![Total Downloads](https://camo.githubusercontent.com/83a294e0d6e7edbca1008fc6d484d33b19cc737d152a48d281688d1e13edc49b/68747470733a2f2f706f7365722e707567782e6f72672f6d79636c6162732f7068702d656e756d2f646f776e6c6f6164732e706e67)](https://packagist.org/packages/myclabs/php-enum)[![psalm](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
 */
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 = 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:

- `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
- `search()` Return key for searched value

### Static methods

[](#static-methods)

```
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:

```
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()
 */
class Action extends Enum
{
    private const VIEW = 'view';
    private const EDIT = 'edit';
}
```

Related projects
----------------

[](#related-projects)

- [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)
- [Yii2 enum mapping](https://github.com/KartaviK/yii2-enum)

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 51.7% 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 ~0 days

Total

2

Last Release

1338d ago

PHP version history (2 changes)1.7.7.1PHP &gt;=7.1

1.8PHP ^7.3 || ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2717384?v=4)[René Schleusner](/maintainers/Schleuse)[@Schleuse](https://github.com/Schleuse)

---

Top Contributors

[![mnapoli](https://avatars.githubusercontent.com/u/720328?v=4)](https://github.com/mnapoli "mnapoli (89 commits)")[![KartaviK](https://avatars.githubusercontent.com/u/5941637?v=4)](https://github.com/KartaviK "KartaviK (12 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)")[![BernardoSilva](https://avatars.githubusercontent.com/u/1537510?v=4)](https://github.com/BernardoSilva "BernardoSilva (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)")[![Schleuse](https://avatars.githubusercontent.com/u/2717384?v=4)](https://github.com/Schleuse "Schleuse (3 commits)")[![KKKas](https://avatars.githubusercontent.com/u/60153?v=4)](https://github.com/KKKas "KKKas (2 commits)")[![Ex3v](https://avatars.githubusercontent.com/u/2785665?v=4)](https://github.com/Ex3v "Ex3v (1 commits)")[![drmonkeyninja](https://avatars.githubusercontent.com/u/357623?v=4)](https://github.com/drmonkeyninja "drmonkeyninja (1 commits)")[![drealecs](https://avatars.githubusercontent.com/u/209984?v=4)](https://github.com/drealecs "drealecs (1 commits)")[![osavchenko](https://avatars.githubusercontent.com/u/617002?v=4)](https://github.com/osavchenko "osavchenko (1 commits)")[![Danack](https://avatars.githubusercontent.com/u/1505719?v=4)](https://github.com/Danack "Danack (1 commits)")

---

Tags

enum

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[myclabs/php-enum

PHP Enum implementation

2.7k227.9M637](/packages/myclabs-php-enum)[dasprid/enum

PHP 7.1 enum implementation

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

PHP Enums

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

Simple and fast implementation of enumerations with native PHP

49444.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)

PHPackages © 2026

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