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

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

dasprid/enum
============

PHP 7.1 enum implementation

1.0.7(8mo ago)382146.0M—0.3%1410BSD-2-ClausePHPPHP &gt;=7.1 &lt;9.0CI failing

Since Oct 25Pushed 8mo ago2 watchersCompare

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

READMEChangelogDependencies (2)Versions (9)Used By (10)

PHP 7.1 enums
=============

[](#php-71-enums)

[![Build Status](https://github.com/DASPRiD/Enum/actions/workflows/tests.yml/badge.svg)](https://github.com/DASPRiD/Enum/actions?query=workflow%3Atests)[![Coverage Status](https://camo.githubusercontent.com/06f582a79817bd51d904a8aaa6262d43386f9986a5b30dc47086a09abd137b0c/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f444153505269442f456e756d2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/DASPRiD/Enum?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/3d0d202fe7a353ce29eb2831d8b3d54ec93f4157e93a9a06f633910d24a10cbe/68747470733a2f2f706f7365722e707567782e6f72672f646173707269642f656e756d2f762f737461626c65)](https://packagist.org/packages/dasprid/enum)[![Total Downloads](https://camo.githubusercontent.com/981e96c83c0d375a9bd64da0e33843e52faf6f0edeb9e7c715d575ce8e16e4f8/68747470733a2f2f706f7365722e707567782e6f72672f646173707269642f656e756d2f646f776e6c6f616473)](https://packagist.org/packages/dasprid/enum)[![License](https://camo.githubusercontent.com/b8b947eb9663886557ed29b25e4bd5402b67f7811748e4da15e29a2b28296807/68747470733a2f2f706f7365722e707567782e6f72672f646173707269642f656e756d2f6c6963656e7365)](https://packagist.org/packages/dasprid/enum)

It is a well known fact that PHP is missing a basic enum type, ignoring the rather incomplete `SplEnum` implementation which is only available as a PECL extension. There are also quite a few other userland enum implementations around, but all of them have one or another compromise. This library tries to close that gap as far as PHP allows it to.

Usage
-----

[](#usage)

### Basics

[](#basics)

At its core, there is the `DASPRiD\Enum\AbstractEnum` class, which by default will work with constants like any other enum implementation you might know. The first clear difference is that you should define all the constants as protected (so nobody outside your class can read them but the `AbstractEnum` can still do so). The other even mightier difference is that, for simple enums, the value of the constant doesn't matter at all. Let's have a look at a simple example:

```
use DASPRiD\Enum\AbstractEnum;

/**
 * @method static self MONDAY()
 * @method static self TUESDAY()
 * @method static self WEDNESDAY()
 * @method static self THURSDAY()
 * @method static self FRIDAY()
 * @method static self SATURDAY()
 * @method static self SUNDAY()
 */
final class WeekDay extends AbstractEnum
{
    protected const MONDAY = null;
    protected const TUESDAY = null;
    protected const WEDNESDAY = null;
    protected const THURSDAY = null;
    protected const FRIDAY = null;
    protected const SATURDAY = null;
    protected const SUNDAY = null;
}
```

If you need to provide constants for either internal use or public use, you can mark them as either private or public, in which case they will be ignored by the enum, which only considers protected constants as valid values. As you can see, we specifically defined the generated magic methods in a class level doc block, so anyone using this class will automatically have proper auto-completion in their IDE. Now since you have defined the enum, you can simply use it like that:

```
function tellItLikeItIs(WeekDay $weekDay)
{
    switch ($weekDay) {
        case WeekDay::MONDAY():
            echo 'Mondays are bad.';
            break;

        case WeekDay::FRIDAY():
            echo 'Fridays are better.';
            break;

        case WeekDay::SATURDAY():
        case WeekDay::SUNDAY():
            echo 'Weekends are best.';
            break;

        default:
            echo 'Midweek days are so-so.';
    }
}

tellItLikeItIs(WeekDay::MONDAY());
tellItLikeItIs(WeekDay::WEDNESDAY());
tellItLikeItIs(WeekDay::FRIDAY());
tellItLikeItIs(WeekDay::SATURDAY());
tellItLikeItIs(WeekDay::SUNDAY());
```

### More complex example

[](#more-complex-example)

Of course, all enums are singletons, which are not cloneable or serializable. Thus you can be sure that there is always just one instance of the same type. Of course, the values of constants are not completely useless, let's have a look at a more complex example:

```
use DASPRiD\Enum\AbstractEnum;

/**
 * @method static self MERCURY()
 * @method static self VENUS()
 * @method static self EARTH()
 * @method static self MARS()
 * @method static self JUPITER()
 * @method static self SATURN()
 * @method static self URANUS()
 * @method static self NEPTUNE()
 */
final class Planet extends AbstractEnum
{
    protected const MERCURY = [3.303e+23, 2.4397e6];
    protected const VENUS = [4.869e+24, 6.0518e6];
    protected const EARTH = [5.976e+24, 6.37814e6];
    protected const MARS = [6.421e+23, 3.3972e6];
    protected const JUPITER = [1.9e+27, 7.1492e7];
    protected const SATURN = [5.688e+26, 6.0268e7];
    protected const URANUS = [8.686e+25, 2.5559e7];
    protected const NEPTUNE = [1.024e+26, 2.4746e7];

    /**
     * Universal gravitational constant.
     *
     * @var float
     */
    private const G = 6.67300E-11;

    /**
     * Mass in kilograms.
     *
     * @var float
     */
    private $mass;

    /**
     * Radius in meters.
     *
     * @var float
     */
    private $radius;

    protected function __construct(float $mass, float $radius)
    {
        $this->mass = $mass;
        $this->radius = $radius;
    }

    public function mass() : float
    {
        return $this->mass;
    }

    public function radius() : float
    {
        return $this->radius;
    }

    public function surfaceGravity() : float
    {
        return self::G * $this->mass / ($this->radius * $this->radius);
    }

    public function surfaceWeight(float $otherMass) : float
    {
        return $otherMass * $this->surfaceGravity();
    }
}

$myMass = 80;

foreach (Planet::values() as $planet) {
    printf("Your weight on %s is %f\n", $planet, $planet->surfaceWeight($myMass));
}
```

###  Health Score

67

—

FairBetter than 100% of packages

Maintenance61

Regular maintenance activity

Popularity73

Solid adoption and visibility

Community28

Small or concentrated contributor base

Maturity86

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

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

Recently: every ~452 days

Total

8

Last Release

244d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0490627b04e8600f8227138bac3d4aea04c22eecfa119e73b47c362363904847?d=identicon)[DASPRiD](/maintainers/DASPRiD)

---

Top Contributors

[![DASPRiD](https://avatars.githubusercontent.com/u/233300?v=4)](https://github.com/DASPRiD "DASPRiD (14 commits)")[![williamdes](https://avatars.githubusercontent.com/u/7784660?v=4)](https://github.com/williamdes "williamdes (9 commits)")[![Chris53897](https://avatars.githubusercontent.com/u/7104259?v=4)](https://github.com/Chris53897 "Chris53897 (1 commits)")[![ausi](https://avatars.githubusercontent.com/u/367169?v=4)](https://github.com/ausi "ausi (1 commits)")[![phpfui](https://avatars.githubusercontent.com/u/7434059?v=4)](https://github.com/phpfui "phpfui (1 commits)")[![remicollet](https://avatars.githubusercontent.com/u/270445?v=4)](https://github.com/remicollet "remicollet (1 commits)")[![pabzm](https://avatars.githubusercontent.com/u/57864086?v=4)](https://github.com/pabzm "pabzm (1 commits)")[![carusogabriel](https://avatars.githubusercontent.com/u/16328050?v=4)](https://github.com/carusogabriel "carusogabriel (1 commits)")

---

Tags

enummap

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[myclabs/php-enum

PHP Enum implementation

2.7k227.9M637](/packages/myclabs-php-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49644.8M97](/packages/marc-mabe-php-enum)[phpcollection/phpcollection

General-Purpose Collection Library for PHP

1.0k64.0M34](/packages/phpcollection-phpcollection)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)[aimeos/map

Easy and elegant handling of PHP arrays as array-like collection objects similar to jQuery and Laravel Collections

4.2k412.9k11](/packages/aimeos-map)[spatie/geocoder

Geocoding addresses to coordinates

8404.8M15](/packages/spatie-geocoder)

PHPackages © 2026

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