PHPackages                             mediagone/types-enums - 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. mediagone/types-enums

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

mediagone/types-enums
=====================

Strongly typed enum (classes) to mimic PHP's 8.1 enums.

0.1.5(4y ago)02.0k1MITPHPPHP ^7.4|^8.0

Since Apr 24Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Mediagone/types-enums)[ Packagist](https://packagist.org/packages/mediagone/types-enums)[ RSS](/packages/mediagone-types-enums/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (1)Versions (7)Used By (1)

Types Enums
===========

[](#types-enums)

[![Latest Version on Packagist](https://camo.githubusercontent.com/22b6e250eb7229651a9c1c9d9ce453d973849aa5e2979269b4f5fd6f1ad4e44d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d65646961676f6e652f74797065732d656e756d732e737667)](https://packagist.org/packages/mediagone/types-enums)[![Total Downloads](https://camo.githubusercontent.com/637b7552850ab3398923bc6f4270efcbba6805e2478888312a5bb44eab177fef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d65646961676f6e652f74797065732d656e756d732e737667)](https://packagist.org/packages/mediagone/types-enums)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)

Installation
------------

[](#installation)

This package requires **PHP 7.4+**

Add it as Composer dependency:

```
$ composer require mediagone/types-enums
```

Introduction
------------

[](#introduction)

Using constants to represent enumerable values is unsafe unless we provide sufficient checks to prevent any problem:

```
class Article
{
    public const STATUS_DRAFT = 0;
    public const STATUS_PUBLISHED = 1;

    private int $status;

    public function changeStatus(int $status) : void
    {
        // We need need to check if the provided status value is valid.
        if (! in_array($status, [Article::STATUS_DRAFT, Article::STATUS_PUBLISHED], true)) {
            throw new LogicException("Invalid status ($status)");
        }

        $this->status = $status;
    }
}

$article->changeStatus(Article::STATUS_PUBLISHED); // valid
$article->changeStatus(1); // valid but not safe
$article->changeStatus(2); // invalid (and risky if there is no validity check in the method)

class OtherClass
{
    public const ANOTHER_INT_CONSTANT = 0;
}

$article->changeStatus(OtherClass::ANOTHER_INT_CONSTANT); // valid but senseless
```

Using **strongly typed Enums** instead of PHP primitive types (int, string) allows to safely typehint them everywhere and ensure that your data is valid without adding any check in your code.

Unfortunately, PHP doesn't provide native enums prior to 8.1, but they can be emulated using classes.

Documentation
-------------

[](#documentation)

### Simple usage

[](#simple-usage)

First of all, create an enum class that defines your enumerable values as **private constants**:
*Note: add static method annotations to your class to enable autocompletion in your favorite IDE.*

```
/**
 * @method static ArticleStatus DRAFT()
 * @method static ArticleStatus PUBLISHED()
 */
final class ArticleStatus extends EnumInt
{
    private const DRAFT = 0;
    private const PUBLISHED = 1;
}
```

Enum values are now accessible using static methods which return an instance of `ArticleStatus`:

```
ArticleStatus::DRAFT();
ArticleStatus::PUBLISHED();

// Strict comparisons are valid since methods always return the same instance
ArticleStatus::PUBLISHED() === ArticleStatus::PUBLISHED(); // true
ArticleStatus::DRAFT() === ArticleStatus::PUBLISHED(); // false
```

Now, you can use it in your class as a regular typed property:

```
class Article
{
    private ArticleStatus $status;

    public function changeStatus(ArticleStatus $status) : void
    {
        // No check needed because $status is always a valid value.
        $this->status = $status;
    }
}

$article->changeStatus(ArticleStatus::PUBLISHED()); // valid
$article->changeStatus(1); // invalid
```

### Enum informations

[](#enum-informations)

You can access enum underlying value and name by using `->value` and `->name` properties:

```
final class ArticleStatus extends EnumInt
{
    private const DRAFT = 0;
    private const PUBLISHED = 1;
}

ArticleStatus::PUBLISHED()->value; // 1
ArticleStatus::PUBLISHED()->name; // "PUBLISHED"
```

### Serialization

[](#serialization)

Because the PHP serialization mechanism doesn't allow to define which instance is restored, it totally breaks how the library works and strict comparison:

```
$a = unserialize(serialize(ArticleStatus::PUBLISHED()));
$b = ArticleStatus::PUBLISHED();

$a === $b; // false
```

That's why `serialize()` and `unserialize()` are blocked for all enums classes.

If you need to deal with serialization, you'll have to store the **enum's value** and restore it afterward manually:

```
$serializedValue = ArticleStatus::PUBLISHED()->value;

// restore the enum
$enum = ArticleStatus::from($serializedValue);
```

*Note: using enum's name for serialization is not recommended since code refactoring might break it anytime.*

License
-------

[](#license)

*Types Enums* is licensed under MIT license. See LICENSE file.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity51

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

Every ~44 days

Recently: every ~55 days

Total

6

Last Release

1616d ago

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

enumphpenumenums

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mediagone-types-enums/health.svg)

```
[![Health](https://phpackages.com/badges/mediagone-types-enums/health.svg)](https://phpackages.com/packages/mediagone-types-enums)
```

###  Alternatives

[myclabs/php-enum

PHP Enum implementation

2.7k227.9M634](/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)[cerbero/enum

Zero-dependencies package to supercharge enum functionalities.

359207.5k2](/packages/cerbero-enum)

PHPackages © 2026

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