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. [Utility &amp; Helpers](/categories/utility)
4. /
5. dkulyk/enum

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

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

PHP Enum Laravel integration

1.3.0(3mo ago)0168MITPHP

Since Mar 5Pushed 3mo ago1 watchersCompare

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

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

43

—

FairBetter than 90% of packages

Maintenance78

Regular maintenance activity

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

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

116d 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

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[illuminate/collections

The Illuminate Collections package.

27075.6M1.0k](/packages/illuminate-collections)[spatie/laravel-enum

Laravel Enum support

3655.7M34](/packages/spatie-laravel-enum)[illuminate/config

The Illuminate Config package.

10943.9M2.4k](/packages/illuminate-config)[illuminate/pipeline

The Illuminate Pipeline package.

9348.3M267](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10533.5M989](/packages/illuminate-pagination)

PHPackages © 2026

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