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

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

richard-ejem/enum
=================

Ultralightweight enum type for PHP

1.0(9y ago)04.5k↓80%BSD-3-ClausePHPPHP &gt;=7

Since Jun 21Pushed 9y ago1 watchersCompare

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

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

PHP lightweight Enum trait
==========================

[](#php-lightweight-enum-trait)

[![Build Status](https://camo.githubusercontent.com/ba108c44ac12c7657045ee922c45a8f45eb4e1eecb4b3b43498d3b99b4f6b39f/68747470733a2f2f7472617669732d63692e6f72672f726963686172642d656a656d2f656e756d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/richard-ejem/enum)[![Downloads this Month](https://camo.githubusercontent.com/a5ac3c263357b7d194f787a1249f5e0949589bc7c321cdcdcac247f3751ea0a7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f726963686172642d656a656d2f656e756d2e737667)](https://packagist.org/packages/richard-ejem/enum)[![Latest stable](https://camo.githubusercontent.com/ff4158b50c5019d551bb8e06bbdb7e70ad0079bbf1a28ee1fd6103758d1c782c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726963686172642d656a656d2f656e756d2e737667)](https://packagist.org/packages/richard-ejem/enum)

Simple trait to create enumeration classes.

Separate your enumerations into standalone classes and validate through your application that you are passing valid enumeration values.

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

[](#installation)

Recommended to install using composer:

`composer require richard-ejem/enum`

Example
-------

[](#example)

```
class UserRole
{
    use \Ejem\Enum\EnumTrait;

    const GUEST = 'guest';
    const USER = 'user';
    const ADMIN = 'admin';
}

class User
{
    /** @var string UserRole enum */
    private $role;

    /** @throws \Ejem\Enum\InvalidEnumValueException */
    public function __construct(string $role)
    {
        $this->setRole($role);
    }

    public function getRole(): string
    {
        return $this->role;
    }

    /** @throws \Ejem\Enum\InvalidEnumValueException */
    public function setRole(string $role): void
    {
        UserRole::assertValidValue($role);
        $this->role = $role;
    }

    // ... more user stuff
}
```

Other features
--------------

[](#other-features)

```
// Validate without throwing exception.
// imagine you are importing users from an old system,
// setting all unsupported roles to GUEST:

$role = UserRole::isValidValue($row['role']) ? $row['role'] : UserRole::GUEST;

// Retrieve all possible values.
// Assign random color to a shape

$values = Color::getPossibleValues();
$shape->setColor($values[mt_rand(0, count($values)-1)]);
```

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

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

Unknown

Total

1

Last Release

3289d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/883765?v=4)[amik](/maintainers/amik)[@amik](https://github.com/amik)

---

Top Contributors

[![richard-ejem](https://avatars.githubusercontent.com/u/7109726?v=4)](https://github.com/richard-ejem "richard-ejem (6 commits)")

---

Tags

phpenum

### Embed Badge

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

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

###  Alternatives

[kongulov/interact-with-enum

Trait for convenient use of ENUM in PHP

3057.9k2](/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.

2518.6k](/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.9k](/packages/ducks-project-spl-types)

PHPackages © 2026

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