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

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

madison-solutions/enum
======================

A system for defining something similar to Java or Swift Enum types in PHP

v2.0.0(6y ago)05441BSD-2-ClausePHPPHP ^7.1

Since Oct 29Pushed 6y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (6)Used By (1)

PHP Enum
========

[](#php-enum)

System for defining something similar to Java or Swift Enum types in PHP.

Example Use
-----------

[](#example-use)

Define an Enum by creating a class which extends from \\MadisonSolutions\\Enum, and then define the possible Enum members with the `definitions()` method.

You can define any other methods you like in the class - in the example below, there's an additional `charge()` method for calculating shipping charges.

Note that the constructor method is private - instances of the Enum class cannot be created directly.

```
class ShippingMethod extends \MadisonSolutions\Enum\Enum
{
    public static function definitions() : array
    {
        return [
            'collection' => [
                'label' => 'Collection',
                'description' => 'Collect in person from our store.',
                'fixed_charge' => 0,
                'charge_per_kg' => 0,
            ],
            'van' => [
                'label' => 'Local van delivery',
                'description' => 'Local delivery by our own vans.',
                'fixed_charge' => 10,
                'charge_per_kg' => 0,
            ],
            'courier' => [
                'label' => 'Nationwide delivery',
                'description' => 'Nationwide delivery by courier service.',
                'fixed_charge' => 5,
                'charge_per_kg' => 3,
            ],
        ];
    }

    public function charge($weight) {
        return $this->fixed_charge + $weight * $this->charge_per_kg;
    }
}

```

#### Loop over the members

[](#loop-over-the-members)

```
foreach (ShippingMethods::members() as $method) {
    echo $method->label . ': ' . $method->charge($weight) . "\n";
}

```

#### Get a subset of the members

[](#get-a-subset-of-the-members)

```
$freeMethods = ShippingMethods::subset(function ($method) {
    return $method->fixed_charge === 0 && $method->charge_per_kg === 0;
});

```

#### Access an instance

[](#access-an-instance)

You should not directly create instances of any Enum class - instead use static methods on the class.

```
$van = ShippingMethods::van();
// or
$van = ShippingMethod::named('van');

```

#### Create an instance from untrusted input

[](#create-an-instance-from-untrusted-input)

```
$method = ShippingMethod::maybeNamed($_POST['shipping-method']);
if (! $method) {
    die("No such shipping method {$_POST['shipping-method']}");
}

```

#### Compare instances

[](#compare-instances)

```
if ($method == ShippingMethods::collection()) {
    echo "You are collecting in person.";
}
// or
if ($method->name == 'collection') {
    echo "You are collecting in person.";
}
// or
switch ($method) {
    case ShippingMethods::collection():
        echo "You are collecting in person.";
        break;
}

```

#### Load from / store to database

[](#load-from--store-to-database)

```
// store
$stringValue = ShippingMethods::nameOf($method);

// save $stringValue in database

// load
$method = ShippingMethod::maybeNamed($stringValue);

```

Alternatively the serialize and unserialize methods can be used, however unserialize will throw an UnexpectedValueException if the serialized member no longer exists (because for example the definitions were changed between the time the data was saved, and when it was restored).

#### Type Hint

[](#type-hint)

```
function validateShippingMethod($order, ShippingMethod $method) {
    if ($method === ShippingMethods::van()) {
        if (! $order->address->isLocal()) {
            die("Van delivery is only available for local addresses");
        }
    }
}

```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

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

Total

5

Last Release

2541d ago

Major Versions

v1.x-dev → v2.0.02019-05-30

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

enum

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/madison-solutions-enum/health.svg)](https://phpackages.com/packages/madison-solutions-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)
