PHPackages                             jac/enums - 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. jac/enums

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

jac/enums
=========

Php implementation of Enums

1.1.0(5y ago)06.2k↓61.5%MITPHPPHP &gt;=7.2

Since Mar 13Pushed 5y ago1 watchersCompare

[ Source](https://github.com/KpnQ/Enums)[ Packagist](https://packagist.org/packages/jac/enums)[ RSS](/packages/jac-enums/feed)WikiDiscussions main Synced yesterday

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

[![Build Status](https://camo.githubusercontent.com/97af7329fed447269d47dbb4f4d8c5a261b44adad37520fb54dc010ca9a932d6/68747470733a2f2f7472617669732d63692e636f6d2f4b706e512f456e756d732e7376673f6272616e63683d6d61696e)](https://travis-ci.com/KpnQ/Enums)[![psalm](https://camo.githubusercontent.com/eb6d00be47748dc80ae900c870b78491ff560c2fb5dc8a38b175c2f6480d62b3/68747470733a2f2f73686570686572642e6465762f6769746875622f4b706e512f456e756d732f636f7665726167652e737667)](https://shepherd.dev/githubKpnQ/Enums)[![Coverage Status](https://camo.githubusercontent.com/6c6f74f1a8967bb2e88dfdef684eda33161a70ed9a0cd2e028902cd1c40840c4/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4b706e512f456e756d732f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/KpnQ/Enums?branch=main)[![Mutation testing badge](https://camo.githubusercontent.com/f4ff2a85cdbbce896e526d9cd35fc3f029ad8f7cf34c0e39cd76b05e8b99476b/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d2532464b706e51253246456e756d732532466d61696e)](https://dashboard.stryker-mutator.io/reports/github.com/KpnQ/Enums/main)

Enum implementation for PHP
===========================

[](#enum-implementation-for-php)

Why
---

[](#why)

To avoid to have to install the `SplEnum`, yet another extension...

Enums have a lot of benefits (in my own opinion):

- Allow type hinting
- Don't need to search in endless array definitions
- Avoid misspelling of values
- Can be enrich with methods

In this implementation the constructor is made private to avoid duplicating instance of the enum

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

[](#installation)

```
composer require jac/php-enum

```

How to use
----------

[](#how-to-use)

For more complex example look at the **example** directory

### Create an Enum

[](#create-an-enum)

```
use Jac\Enum\AbstractEnum;

/**
 * SendingStatus Enum
 * @method SendingStatus PROCESSING()
 * @method SendingStatus SENT()
 */
final class SendingStatus extends AbstractEnum
{
    private const PROCESSING = '1 - Processing';
    private const SENT = '2 - Sent';
}
```

### Usage

[](#usage)

```
function notifyUser(SendingStatus $status) {
    if ($status == SendingStatus::SENT()) {
        // your package have been sent
    }
}

$key = 'PROCESSING';
// Validate first the enum by inEnum
if (SendingStatus::inEnum($key)) {
    notifyUser(SendingStatus::$key());
}
```

### Enum having multiple keys with the same value\[Multiple\]

[](#enum-having-multiple-keys-with-the-same-valuemultiple)

Instantiate an Enum from its value is possible calling the `Devise::EUR()` for example, and having multiple keys with the same value won't be harmful in that case. However, it is different when we try to instantiate an Enum from its value. Let's take the following example:

```
namespace App\Enums;

use Jac\Enum\AbstractEnum;

final class Devise extends AbstractEnum
{
    /**
     * @default
     */
    private const RMB = 'RMB'; // will be used due to the default tag
    private const CNY = 'RMB';

    private const EUR = 'EUR'; // will be used as the other key is deprecated
    /**
     * @deprecated
     */
    private const FRA = 'EUR';

    private const US_DOLLAR = 'USD'; // will be used because of the __DEFAULT__ configuration
    private const USD = 'USD';

    private const __DEFAULT__ = array(
        'USD' => 'US_DOLLAR'
    );
}
```

There is multiple ways a developer could use to help the builder decide which key should be preferred.

1. Use the `__DEFAULT__` constant which is a reverted key =&gt; value array. This method will first be checked before the following two
2. Use the `@default` tag in the php doc associated to the constant, in the above example, for the chinese devise (RMB, CNY), in case we use `Devise::from('RMB')`, the key RMB is going to be choose.
3. Use the `@deprecated` to exclude values. When a constant is set as deprecated, then its priority is lowered and it will be returned if and only if no other options is found.

In case there is no configuration set for the default value to choose or if there is still multiple value available, a warning is triggered and one of the found options is returned.

API
---

[](#api)

Comparison of enums works with == and ===

- `__toString()` To display the current enum : EnumName::EnumKey::EnumValue (can be override)
- `getValue()` Return the value of the Enum (the value of the constant associated with the current key)
- `getKey()` Return the key of the the current Enum
- `equals(AbstractEnum|null):bool` Compare both enums using their values without taking the key into account.

Static method

- `enum(string)` Create an instance of the enum giving its key
- `from(mixed)` Create an enum from its value, see \[Multiple\] to understand more about its behavior
- `toArray()` return the list of key =&gt; value of the enum
- `inEnum()` check if the parameter is either a key or a value
- `search(mixed): array` Search for all the keys having the given value
- `keyExists(string): bool` Check if the key exists in the enum
- `valueExists(mixed): bool` Check if at leas one of the keys have the given value
- `keys(): array`: The list of available keys
- `values(): array`: The list of available none unique values

Serialization
-------------

[](#serialization)

The enum implements the `JsonSerializable` interface. By default it will return the value as a string

A formatter is available: `Jac\Enum\EnumJsonFormat` with several option implemented.

```
use Jac\Enum\EnumJsonFormat;

echo json_decode(EnumJsonFormat::asKeyValue()->format(Devise::USD_DOLLAR()));
```

Will output

```
{
    "USD_DOLLAR":"USD"
}
```

```
use Jac\Enum\EnumJsonFormat;

echo json_decode(EnumJsonFormat::keyAndValueAsValues()->format(Devise::USD_DOLLAR()));
```

Will output

```
{
    "App\\Enums\\Devise": {
        "key":"USD_DOLLAR",
        "value":"USD"
    }
}
```

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 69.4% 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

Unknown

Total

1

Last Release

1938d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7d6300093a8eaf7d279e8a10585a5ccb148b57afd24d3b8e2289ed6f0de0c27f?d=identicon)[KpnQ](/maintainers/KpnQ)

---

Top Contributors

[![JimmyAstruc](https://avatars.githubusercontent.com/u/8327237?v=4)](https://github.com/JimmyAstruc "JimmyAstruc (25 commits)")[![KpnQ](https://avatars.githubusercontent.com/u/47003898?v=4)](https://github.com/KpnQ "KpnQ (11 commits)")

---

Tags

enums

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jac-enums/health.svg)

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

###  Alternatives

[brysem/phpenums

Enums made simple in PHP.

10177.6k](/packages/brysem-phpenums)[defstudio/enum-features

A simple trait to enable a feature system using Enums and Laravel Pennant

162.2k](/packages/defstudio-enum-features)[sakanjo/laravel-easy-enum

Easily work with enum.

101.6k1](/packages/sakanjo-laravel-easy-enum)

PHPackages © 2026

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