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

ActiveLibrary

dkulyk/enum
===========

PHP Enum Laravel integration

1.3.0(3w ago)0166↓100%MITPHP

Since Mar 5Pushed 3w ago1 watchersCompare

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

READMEChangelogDependencies (2)Versions (13)Used By (0)

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

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

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 dkulyk/enum

```

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

[](#declaration)

```
use DKulyk\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

47

—

FairBetter than 94% of packages

Maintenance94

Actively maintained with recent releases

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

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 ~198 days

Recently: every ~524 days

Total

12

Last Release

25d ago

PHP version history (2 changes)1.0.0PHP &gt;=7.0.0

1.1.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/8e3e16dc7317cb0199e7b210ce186227060bf603c2c64bb34b1b2ef003fafd90?d=identicon)[lnkvisitor](/maintainers/lnkvisitor)

---

Top Contributors

[![dkulyk](https://avatars.githubusercontent.com/u/370042?v=4)](https://github.com/dkulyk "dkulyk (5 commits)")

---

Tags

enum

### Embed Badge

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

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

###  Alternatives

[myclabs/php-enum

PHP Enum implementation

2.7k225.2M600](/packages/myclabs-php-enum)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.6M96](/packages/bensampo-laravel-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49340.7M89](/packages/marc-mabe-php-enum)[spatie/laravel-enum

Laravel Enum support

3645.3M29](/packages/spatie-laravel-enum)[timeweb/phpstan-enum

Enum class reflection extension for PHPStan

443.2M19](/packages/timeweb-phpstan-enum)[cerbero/laravel-enum

Laravel package to supercharge enum functionalities.

18986.6k](/packages/cerbero-laravel-enum)

PHPackages © 2026

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