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

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

slava-basko/enum
================

PHP enum implementation

1.0.0(3y ago)07.2k↓23.8%1BSD-2-ClausePHP

Since Aug 7Pushed 3y agoCompare

[ Source](https://github.com/slava-basko/Enum)[ Packagist](https://packagist.org/packages/slava-basko/enum)[ RSS](/packages/slava-basko-enum/feed)WikiDiscussions master Synced 2d ago

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

PHP enums
=========

[](#php-enums)

Please, use PHP built-in Enum if you are on PHP 8.1+.

This is a port of  for PHP 5 because there still exist services/apps in production that using old PHP, unfortunately. But they also want a bit of Enum 🙂.

It is a well known fact that PHP is missing a basic enum type before version 8.1, 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 (before version 8.1).

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 static $MONDAY;
    protected static $TUESDAY;
    protected static $WEDNESDAY;
    protected static $THURSDAY;
    protected static $FRIDAY;
    protected static $SATURDAY;
    protected static $SUNDAY;
}
```

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 static $MERCURY = [3.303e+23, 2.4397e6];
    protected static $VENUS = [4.869e+24, 6.0518e6];
    protected static $EARTH = [5.976e+24, 6.37814e6];
    protected static $MARS = [6.421e+23, 3.3972e6];
    protected static $JUPITER = [1.9e+27, 7.1492e7];
    protected static $SATURN = [5.688e+26, 6.0268e7];
    protected static $URANUS = [8.686e+25, 2.5559e7];
    protected static $NEPTUNE = [1.024e+26, 2.4746e7];

    /**
     * Universal gravitational constant.
     */
    private static $G = 6.67300E-11;

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

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

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

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

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

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

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

$myMass = 80;

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

### How to run tests

[](#how-to-run-tests)

Install dependencies

```
docker run -v `pwd`:/var/www --rm feitosa/php55-with-composer composer install
```

Run tests

```
docker run -v `pwd`:/var/www --rm feitosa/php55-with-composer vendor/bin/phpunit
```

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 52.4% 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

1427d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6a8e37b4c52b1cfe4f5d64f420d21875ebdeb53954d6859d6d663038b95ef3cc?d=identicon)[slava-basko](/maintainers/slava-basko)

---

Top Contributors

[![DASPRiD](https://avatars.githubusercontent.com/u/233300?v=4)](https://github.com/DASPRiD "DASPRiD (11 commits)")[![williamdes](https://avatars.githubusercontent.com/u/7784660?v=4)](https://github.com/williamdes "williamdes (7 commits)")[![ausi](https://avatars.githubusercontent.com/u/367169?v=4)](https://github.com/ausi "ausi (1 commits)")[![carusogabriel](https://avatars.githubusercontent.com/u/16328050?v=4)](https://github.com/carusogabriel "carusogabriel (1 commits)")[![remicollet](https://avatars.githubusercontent.com/u/270445?v=4)](https://github.com/remicollet "remicollet (1 commits)")

---

Tags

enummap

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[myclabs/php-enum

PHP Enum implementation

2.7k236.1M695](/packages/myclabs-php-enum)[dasprid/enum

PHP 7.1 enum implementation

382165.7M12](/packages/dasprid-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

50458.3M110](/packages/marc-mabe-php-enum)[phpcollection/phpcollection

General-Purpose Collection Library for PHP

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

PHP Enums

85232.6M76](/packages/spatie-enum)[spatie/geocoder

Geocoding addresses to coordinates

8555.2M20](/packages/spatie-geocoder)

PHPackages © 2026

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