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

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

spareparts/enum
===============

Simple ENUM class.

v1.1.2(5y ago)329.4k2[1 PRs](https://github.com/SpareParts/Enum/pulls)3MITPHPPHP &gt;=7.1CI failing

Since Jan 12Pushed 5y agoCompare

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

READMEChangelog (10)Dependencies (2)Versions (15)Used By (3)

Enum
====

[](#enum)

PHP Enum done right

[![Build Status](https://camo.githubusercontent.com/b6b5f413acc333f9b89f19d46b0e29175ae10432aa0b51a793525a8a24fe0935/68747470733a2f2f7472617669732d63692e6f72672f537061726550617274732f456e756d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/SpareParts/Enum)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/010a5ad6252177f0f5570b0a8b63981dd98d579492cd12f0cc42cc268e0f2710/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f537061726550617274732f456e756d2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/SpareParts/Enum/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/f930a6764580d11a90955b8149d1b060ff2fbe163d4d0f7ed3715a1ae079bba8/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f537061726550617274732f456e756d2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/SpareParts/Enum/?branch=master)

Easy way to use enumerated values in PHP.

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

[](#installation)

Please, use composer.

```
composer require spareparts/enum
```

Basic usage
-----------

[](#basic-usage)

```
/**
 * @method static OPEN
 * @method static CLOSED
 */
class WindowStateEnum extends \SpareParts\Enum\Enum
{
}

// obtain enum value
$state = WindowStateEnum::OPEN();

// assign enum value
$windows->state = WindowStateEnum::CLOSED();

// compare enum values
if ($window->state === WindowStateEnum::OPEN()) {
    ....
}

// use enum to guard method parameters
function changeWindowState(WindowStateEnum $newState) {
    ...
}
```

### How to prepare Enum

[](#how-to-prepare-enum)

1. extend Enum class
2. Annotate enum class with @method annotations describing Enum values

### How to use Enum

[](#how-to-use-enum)

There are two possible ways to use enum values, with first one being preferred.

1. using static methods with same name as your desired value.

This works with help from magic \_\_callStatic method, meaning you do not have to add any methods manually.

```
$state = WindowStateEnum::OPEN();
```

This method is preferred, as it nicely shows its intended value without having to use weakly guarded strings.

**Important tip**: To have values correctly autocompleted, use @method annotations, like this:

```
/**
 * @method static OPEN
 * @method static CLOSED
 */
class WindowStateEnum extends \SpareParts\Enum\Enum
{
}
```

This way, your IDE should know `WindowStateEnum` has 2 methods OPEN and CLOSE and correctly hint on their names. In case you are using IDE without support for @method annotations, you can always just add those methods "for real" :)

2. using `instance()` method with desired value as instance parameter

```
$state = WindowStateEnum::instance('OPEN');
```

There is nothing wrong with this approach, but mistakes/typos can be easily made.

#### Asking enum for unsupported value

[](#asking-enum-for-unsupported-value)

In case you ask for value that is not in the enum values, an InvalidEnumValueException exception is thrown.

```
try {
    $window->setState(WindowStateEnum::FLYING());
} catch (InvalidEnumValueException $e) {
    echo "This is not a correct state for window to be in!";
}
```

#### Testing for multiple values

[](#testing-for-multiple-values)

You can check for enum belonging to any of an enum group / set like this:

```
if ($state->isAnyOf([Window::OPEN(), Window::CLOSED()]));
```

###  Health Score

36

—

LowBetter than 81% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 79.3% 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 ~157 days

Recently: every ~145 days

Total

11

Last Release

1828d ago

Major Versions

v0.4.0 → v1.02018-12-08

PHP version history (2 changes)v0.1.0PHP &gt;=5.5.0

v1.0PHP &gt;=7.1

### Community

Maintainers

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

---

Top Contributors

[![SpareParts](https://avatars.githubusercontent.com/u/1478737?v=4)](https://github.com/SpareParts "SpareParts (23 commits)")[![northys](https://avatars.githubusercontent.com/u/2878126?v=4)](https://github.com/northys "northys (4 commits)")[![adaamz](https://avatars.githubusercontent.com/u/4347332?v=4)](https://github.com/adaamz "adaamz (1 commits)")[![jhynek](https://avatars.githubusercontent.com/u/10742223?v=4)](https://github.com/jhynek "jhynek (1 commits)")

---

Tags

enum

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[myclabs/php-enum

PHP Enum implementation

2.7k227.9M633](/packages/myclabs-php-enum)[dasprid/enum

PHP 7.1 enum implementation

379146.0M11](/packages/dasprid-enum)[spatie/enum

PHP Enums

84429.1M68](/packages/spatie-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49444.8M97](/packages/marc-mabe-php-enum)[spatie/laravel-enum

Laravel Enum support

3655.4M31](/packages/spatie-laravel-enum)[consistence/consistence

Consistence - consistent approach and additions to PHP's functionality

1831.1M18](/packages/consistence-consistence)

PHPackages © 2026

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