PHPackages                             phps-cans/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. phps-cans/enum

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

phps-cans/enum
==============

PHP Enum implementation

1.6.0(9y ago)012MITPHPPHP &gt;=7.0.0

Since Mar 19Pushed 9y ago2 watchersCompare

[ Source](https://github.com/phps-cans/enum)[ Packagist](https://packagist.org/packages/phps-cans/enum)[ Docs](https://github.com/phps-cans/enum)[ RSS](/packages/phps-cans-enum/feed)WikiDiscussions 1.0 Synced yesterday

READMEChangelog (1)Dependencies (2)Versions (14)Used By (0)

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)

Why?
----

[](#why)

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

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

- You can type-hint: `function setAction(Action $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
{
    const VIEW = 'view';
    const EDIT = 'edit';
}
```

Usage
-----

[](#usage)

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

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

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 type-hint enum values:

```
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
{
    const VIEW = 'view';
    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
{
    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
{
    const VIEW = 'view';
    const EDIT = 'edit';
}
```

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 50% 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 ~108 days

Recently: every ~136 days

Total

14

Last Release

3449d ago

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

1.6.0PHP &gt;=7.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/988fb049e7b031fc14b8ce38b8f5bc3e40dbd6bfb3b5bc7f7061b68c41ba39f1?d=identicon)[xhuberty](/maintainers/xhuberty)

---

Top Contributors

[![mnapoli](https://avatars.githubusercontent.com/u/720328?v=4)](https://github.com/mnapoli "mnapoli (1 commits)")[![xhuberty](https://avatars.githubusercontent.com/u/8350192?v=4)](https://github.com/xhuberty "xhuberty (1 commits)")

---

Tags

enum

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[myclabs/php-enum

PHP Enum implementation

2.7k236.1M684](/packages/myclabs-php-enum)[spatie/enum

PHP Enums

85232.6M74](/packages/spatie-enum)[dasprid/enum

PHP 7.1 enum implementation

382158.8M12](/packages/dasprid-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

50458.3M104](/packages/marc-mabe-php-enum)[spatie/laravel-enum

Laravel Enum support

3685.8M34](/packages/spatie-laravel-enum)[consistence/consistence

Consistence - consistent approach and additions to PHP's functionality

1821.1M18](/packages/consistence-consistence)

PHPackages © 2026

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