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

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

donatorsky/enum
===============

Enumeration library for a PHP 7.1+

1.0.0(8y ago)026MITPHPPHP &gt;=7.1.0

Since Jul 15Pushed 8y ago1 watchersCompare

[ Source](https://github.com/donatorsky/enum)[ Packagist](https://packagist.org/packages/donatorsky/enum)[ Docs](https://github.com/donatorsky/enum)[ RSS](/packages/donatorsky-enum/feed)WikiDiscussions master Synced 3d ago

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

enum
====

[](#enum)

Enumeration library for a PHP 7.1+

An enum type is useful when there is need to allow only one of limited set of values to be accepted. It also helps logically group them in one place.

Its interface is mostly based on [java.lang.Enum](https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html), but implementation adds some more magic to it.

A basic usage:

```
namespace My\App;

/**
 * List of all available days.
 *
 * @method static Day MONDAY()
 * @method static Day TUESDAY()
 * @method static Day WEDNESDAY()
 * @method static Day THURSDAY()
 * @method static Day FRIDAY()
 * @method static Day SATURDAY()
 * @method static Day SUNDAY()
 */
class Day extends \Donatorsky\Enum\Enum
{

    // Make available values private/protected if You want to force using getters
    private const MONDAY    = 1;
    private const TUESDAY   = 2;
    private const WEDNESDAY = 3;
    private const THURSDAY  = 4;
    private const FRIDAY    = 5;
    private const SATURDAY  = 6;
    private const SUNDAY    = 7;

    /**
     * @var int
     */
    protected $value;

    /**
     * Day constructor.
     *
     * @param int $value
     */
    protected function __construct(int $value)
    {
        $this->value = $value;
    }

    /**
     * @return int
     */
    public function getValue(): int
    {
        return $this->value;
    }
}

// Somewhere deep in code...

function getDeveloperState(Day $day): string
{
    switch ($day) {
        case Day::MONDAY():
            return 'Get redy to work';
        break;

        case Day::TUESDAY():
        case Day::WEDNESDAY():
        case Day::THURSDAY():
            return 'Work hard';
        break;

        case Day::FRIDAY():
            return 'Save all work';
        break;

        case Day::SATURDAY():
        case Day::SUNDAY():
            return 'Save all work';
        break;

        // If You add new constants
        default:
            return 'Where am I?!';
        break;
    }
}

// Let's see what to do on friday...
echo getDeveloperState(Day::FRIDAY()) . PHP_EOL;

// Or today
/**
 * In fact fromValue() will always return object of type that was called on
 * @var Day $today
 */
$today = Day::fromValue(date('N'), false);

echo getDeveloperState($today) . PHP_EOL;

// Get constant name of current day
echo $today->name() . PHP_EOL;

// Or check if it is specified day
echo 'Is monday today? ' . ($today->is('MONDAY') ? 'Yes' : 'No') . PHP_EOL;

// Or check it in other way
echo 'Is monday today? ' . ($today->equals(Day::MONDAY()) ? 'Yes' : 'No') . PHP_EOL;

// Tell me who am I...
// Note: Also __toString() uses hashCode(), so "$today" will work unless hashcode() is overwritten
echo $today->hashCode() . PHP_EOL;

// Call (optional) getter of value
echo 'Value of current day is ' . $today->getValue();

// Let's see if there is a day named...
echo 'Is NON_EXISTING_DAY real? ' . (Day::has('NON_EXISTING_DAY') ? 'Yes' : 'No') . PHP_EOL;

// So which days are true?
echo 'Please, choose one of following days: ' . implode(', ', Day::keys()) . PHP_EOL;

// Ok, last question
// Note: valueOf() returns object of Day class
echo 'What is the value of WEDNESDAY? ' . Day::valueOf(Day::class, 'WEDNESDAY')->getValue();
```

Main difference with other implementations is that it is allowed for constants to have complex value:

```
namespace Donatorsky\Enum\Tests;

use Donatorsky\Enum\Enum;

/**
 * Class Rectangle
 *
 * @method static Rectangle SMALL()
 * @method static Rectangle MEDIUM()
 * @method static Rectangle LARGE()
 *
 * @package Donatorsky\Enum\Tests
 */
class Rectangle extends Enum
{

    public const SMALL  = [ 2,  3, 'RED'];
    public const MEDIUM = [ 5,  8, 'GREEN'];
    public const LARGE  = [13, 21, 'BLUE'];

    /**
     * @var float
     */
    protected $width;

    /**
     * @var float
     */
    protected $height;

    /**
     * @var float
     */
    protected $area;

    /**
     * @var Color
     */
    protected $color;

    /**
     * Rectangle constructor.
     *
     * @param float  $width
     * @param float  $height
     * @param string $color
     */
    protected function __construct(float $width, float $height, string $color)
    {
        $this->width = $width;
        $this->height = $height;
        $this->area = $width * $height;
        $this->color = Color::$color();
    }

    /**
     * @return float
     */
    public function getWidth(): float
    {
        return $this->width;
    }

    /**
     * @return float
     */
    public function getHeight(): float
    {
        return $this->height;
    }

    /**
     * @return Color
     */
    public function getColor(): Color
    {
        return $this->color;
    }

    /**
     * @return float
     */
    public function getArea(): float
    {
        return $this->area;
    }
}
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 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

3226d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0d2443c24cf5869099ca69ba917cca98e9c0884a8d5bc29d65fe872a9f54665a?d=identicon)[donatorsky](/maintainers/donatorsky)

---

Top Contributors

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

---

Tags

enum

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[myclabs/php-enum

PHP Enum implementation

2.7k227.9M637](/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)
