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

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

xaamin/enum
===========

PHP Enum implementation

v1.0.4(3y ago)03.2k1MITPHP

Since Feb 28Pushed 3y ago1 watchersCompare

[ Source](https://github.com/xaamin/enum)[ Packagist](https://packagist.org/packages/xaamin/enum)[ RSS](/packages/xaamin-enum/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (6)Used By (1)

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

[](#php-enum-implementation)

This package offers enums in PHP. We don't use a simple "value" representation, so you're always working with the enum object. This allows for proper autocompletion and refactoring in IDEs.

Why?
----

[](#why)

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

- You can enrich the enum with alias methods names (e.g. `draft()`, `published()`, …)
- 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 xaamin/enum

```

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

[](#declaration)

```
use Xaamin\Enum\Enum;

class InvoiceStatus extends Enum
{
    protected $enum = [
        'pending' => 'invoice.pending',
        'paid' => 'invoice.paid',
        'overdue' => 'invoice.overdue',
    ];
}
```

Documentation
-------------

[](#documentation)

- `getName()` Returns the name of the current value on Enum
- `getValue()` Returns the current value of the enum
- `equals($enum)` Tests whether 2 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)
- `search()` Return key for the searched value

### Static methods

[](#static-methods)

```
class InvoiceStatus extends Enum
{
    protected $enum = [
        'pending' => 'invoice.pending',
        'paid' => 'invoice.paid',
        'overdue' => 'invoice.overdue',
    ];
}

// Static method:
$invoice = InvoiceStatus::pending();
$invoice = InvoiceStatus::paid();
```

You can use phpdoc for autocompletion, this is supported in some IDEs:

```
/**
 * @method static self pending()
 * @method static self paid()
 * @method static self overdue()
 */
class InvoiceStatus extends Enum
{
    protected $enum = [
        'pending' => 'invoice.pending',
        'paid' => 'invoice.paid',
        'overdue' => 'invoice.overdue',
    ];
}
```

Usage
-----

[](#usage)

This is how an enum can be defined.

```
/**
 * @method static self pending()
 * @method static self paid()
 * @method static self overdue()
 */
class InvoiceStatus extends Enum
{
    protected $enum = [
        'pending' => 'invoice.pending',
        'paid' => 'invoice.paid',
        'overdue' => 'invoice.overdue',
    ];
}
```

This is how they are used:

```
public function setInvoiceStatus(InvoiceStatus $invoice)
{
    $this->invoice = $invoice;
}

// ...

$class->setInvoiceStatus(InvoiceStatus::paid());
```

This is how get we can get the enum name from a value:

```
$invoice = InvoiceStatus::search('invoice.overdue');

// $invoice is 'overdue'
```

### Creating an enum from a value

[](#creating-an-enum-from-a-value)

```
$invoice = InvoiceStatus::make('paid');
```

### Comparing enums

[](#comparing-enums)

Enums can be compared using the `equals` method:

```
$invoice->equals($invoice);
```

You can also use dynamic `is` methods:

```
// return a boolean
$invoice->isPaid();

// return a boolean
InvoiceStatus::isPaid($invoice);
```

Note that if you want auto completion on these `is` methods, you must add extra doc blocks on your enum classes.

### Search

[](#search)

You can search enum with its textual value:

```
$invoice = InvoiceStatus::search('invoice.pending');

// $invoice is 'pending'
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 87.5% 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 ~233 days

Total

5

Last Release

1338d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ab0364ad60f3e9bba208eb33c139b10281bbead4d267ce0fbd8ed50338174e30?d=identicon)[xaamin](/maintainers/xaamin)

---

Top Contributors

[![benxaamin](https://avatars.githubusercontent.com/u/21373088?v=4)](https://github.com/benxaamin "benxaamin (7 commits)")[![xaamin](https://avatars.githubusercontent.com/u/4313256?v=4)](https://github.com/xaamin "xaamin (1 commits)")

---

Tags

phpenumphp-enum

### Embed Badge

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

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

###  Alternatives

[kongulov/interact-with-enum

Trait for convenient use of ENUM in PHP

3052.3k2](/packages/kongulov-interact-with-enum)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)[ducks-project/spl-types

Polyfill Module for SplType PHP extension. This extension aims at helping people making PHP a stronger typed language and can be a good alternative to scalar type hinting. It provides different typehandling classes as such as integer, float, bool, enum and string

1032.4k](/packages/ducks-project-spl-types)[zlikavac32/php-enum

Better PHP enum support

225.6k5](/packages/zlikavac32-php-enum)

PHPackages © 2026

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