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

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

joskolenberg/enum
=================

Enum implementation for PHP

v1.6.0(1y ago)112.5k↓50%1[1 PRs](https://github.com/joskolenberg/Enum/pulls)MITPHPCI failing

Since Oct 25Pushed 1y agoCompare

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

READMEChangelog (10)Dependencies (2)Versions (10)Used By (0)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/9aee146207113be43426f41378a1057eeaa2f22c9d2228e8489b956ff9ddbd28/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f736b6f6c656e626572672f656e756d2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/joskolenberg/enum/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/c062b387be1cd5d7a8103e8c7ab598f1808abf68880c9ee4c50209972afbccb5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f736b6f6c656e626572672f656e756d2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/joskolenberg/enum/?branch=master)[![Build Status](https://camo.githubusercontent.com/456ea815207077bd4ed24435613ac6d0bb8c6b70a42b390dc05fea6b55484bb4/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f736b6f6c656e626572672f656e756d2f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/joskolenberg/enum/build-status/master)[![Total Downloads](https://camo.githubusercontent.com/cc73ff481550a80aef2d5fd0088c94747602644a856b5a13e597a9f6c746d4fc/68747470733a2f2f706f7365722e707567782e6f72672f6a6f736b6f6c656e626572672f656e756d2f646f776e6c6f616473)](https://packagist.org/packages/joskolenberg/enum)[![Latest Stable Version](https://camo.githubusercontent.com/488a4418ba7442f9822a3cda34420a8e6b0ec3f817e3954569871cc954803dd0/68747470733a2f2f706f7365722e707567782e6f72672f6a6f736b6f6c656e626572672f656e756d2f762f737461626c65)](https://packagist.org/packages/joskolenberg/enum)[![License](https://camo.githubusercontent.com/6478e16ac1c0ee4115dfab9792560feb23c246ffe947b41138d6a3085694c422/68747470733a2f2f706f7365722e707567782e6f72672f6a6f736b6f6c656e626572672f656e756d2f6c6963656e7365)](https://packagist.org/packages/joskolenberg/enum)

Enum
====

[](#enum)

Enum implementation for PHP

Installing
----------

[](#installing)

Use composer to pull this package in.

### With Composer

[](#with-composer)

```
$ composer require joskolenberg/enum

```

Usage
-----

[](#usage)

### Setting up an enum

[](#setting-up-an-enum)

An enum can be created by extending the JosKolenberg\\Enum\\Enum class.

```
use JosKolenberg\Enum\Enum;

class UserType extends Enum {}
```

This created class acts as the repository via static methods as well as the enum itself.

There are two abstract methods which have to be implemented. Use the seed method to populate the class with Enums, and tell the class which attribute identifies the enums by returning the name of this attribute in the identifierAttribute method.

An example implementation would be:

```
use JosKolenberg\Enum\Enum;

class UserType extends Enum
{

    protected $id;

    protected $name;

    public function __construct($id, $name)
    {
        $this->id = $id;
        $this->name = $name;
    }

    /**
     * Seed the class with Enum instances
     *
     * @return array
     */
    protected static function seed()
    {
        return [
            new static('dev', 'Developer'),
            new static('admin', 'Administrator'),
            new static('user', 'User'),
        ];
    }

    /**
     * Return the name of the attribute which stores the identifier
     *
     * @return string
     */
    protected function identifierAttribute()
    {
        return 'id';
    }
}
```

Next, the class can be designed like way you would any other.

### Repository

[](#repository)

The class offers the following static "repository-like" methods:

- `get()` return the enum with the given identifier.
- `random()` return a random identifier (e.g. for seeding).
- `collection()` return an Illuminate\\Support\\Collection containing all enums.
- `identifiers()` return an Illuminate\\Support\\Collection containing only the identifiers for the enums.
- `exists()` tell if an enum with the given identifier exists.

Enums are also available using a magic method with the same name as the identifier. e.g. `UserType::get('dev')` equals `UserType::dev()`

### Enum

[](#enum-1)

The enum itself has the following methods:

- `identifier()` return the identifier value (e.g. 'dev' or 'admin' in the previous example) of this instance.
- `equals(Enum $enum)` check if the given enum matches the current.

All defined attributes are by default available as read-only:

- `$value = $userType->value` passes.
- `$userType->value = 'changed'` fails.

### Custom Classes

[](#custom-classes)

If different enums of the same type should have different behaviour you can extend the base enum to more specific ones.

```
class UserType extends Enum
{

    protected $id;
    protected $name;

    protected static function seed()
    {
        return [
            new UserTypeDev(),
            new UserTypeAdmin(),
        ];
    }

    protected function identifierAttribute()
    {
        return 'id';
    }
}

class UserTypeDev extends UserType{

    public function __construct()
    {
        $this->id = 'dev';
        $this->name = 'Developer';
    }

    public function whatCanYouDo()
    {
        return 'I can code';
    }
}

class UserTypeAdmin extends UserType{

    public function __construct()
    {
        $this->id = 'admin';
        $this->name = 'Administrator';
    }

    public function whatCanYouDo()
    {
        return 'I can do some important stuff';
    }
}
```

If you wish to extend the functionality of the collection, you can assign your own custom collection to your enum using the static newCollection method.

```
    protected static function newCollection(array $enums = [])
    {
        return new MyCustomEnumCollection($enums);
    }
```

### Pre-defined Enums

[](#pre-defined-enums)

Because enums are usually no more than an identifier and maybe a display value, two pre-defined enums are available to extend.

Extend EnumWithId for an enum with only an id attribute:

```
class UserStatus extends \JosKolenberg\Enum\EnumWithId
{
    protected static function seed()
    {
        return [
            new static('new'),
            new static('active'),
            new static('deleted'),
        ];
    }
}
```

Or extend EnumWithIdAndName for an enum with an id and name attribute:

```
class ExtendedUserType extends \JosKolenberg\Enum\EnumWithIdAndName
{
    protected static function seed()
    {
        return [
            new static('dev', 'Developer'),
            new static('admin', 'Administrator'),
            new static('user', 'User'),
        ];
    }
}
```

Or create your own baseclass if you wish for different attributes on all of your enums.

Happy coding!

Jos Kolenberg

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance47

Moderate activity, may be stable

Popularity27

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity69

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 ~341 days

Recently: every ~512 days

Total

9

Last Release

398d ago

Major Versions

v0.1.1 → v1.02018-09-12

PHP version history (3 changes)v1.0PHP &gt;=7.0

v1.1PHP ^7.0

v1.5.0PHP ^7.0||^8.0

### Community

Maintainers

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

---

Top Contributors

[![joskolenberg](https://avatars.githubusercontent.com/u/26161164?v=4)](https://github.com/joskolenberg "joskolenberg (3 commits)")

---

Tags

laravelenumcollections

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-enum

Laravel Enum support

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

Laravel package to supercharge enum functionalities.

18989.6k](/packages/cerbero-laravel-enum)[simplesquid/nova-enum-field

A Laravel Nova field to add enums to resources.

52391.9k2](/packages/simplesquid-nova-enum-field)[datomatic/nova-enum-field

A Laravel Nova PHP 8.1 enum field with filters

20134.2k](/packages/datomatic-nova-enum-field)[nasyrov/laravel-enums

Laravel package for Enum implementation.

3272.5k](/packages/nasyrov-laravel-enums)[lazerg/laravel-enum-pro

A powerful PHP enum extension with collection support, random selection, and magic static calls

4319.0k](/packages/lazerg-laravel-enum-pro)

PHPackages © 2026

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