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

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

mpscholten/typesafe-enum
========================

A lightweight, type safe enum library

1.0.0(10y ago)41.1k2MITPHPCI failing

Since Jul 8Pushed 6y ago2 watchersCompare

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

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

TypesafeEnum
============

[](#typesafeenum)

[![Latest Stable Version](https://camo.githubusercontent.com/e2aa38f8a53a3f0250c34bb64db07268cd3714aecf554e6ff1a5a3c8af3cc409/68747470733a2f2f706f7365722e707567782e6f72672f6d707363686f6c74656e2f74797065736166652d656e756d2f76657273696f6e)](https://packagist.org/packages/mpscholten/typesafe-enum) [![License](https://camo.githubusercontent.com/a45dbcd1983ddc9fab6f476236687a598d28420e78a4987e31030a14b2b2728b/68747470733a2f2f706f7365722e707567782e6f72672f6d707363686f6c74656e2f74797065736166652d656e756d2f6c6963656e7365)](https://packagist.org/packages/mpscholten/typesafe-enum) [![Circle CI](https://camo.githubusercontent.com/6dbc9b88dabf4ca569e9d06e03a17196cafe7e8774cc76877671a233c31e797c/68747470733a2f2f636972636c6563692e636f6d2f67682f6d707363686f6c74656e2f74797065736166652d656e756d2e7376673f7374796c653d736869656c64)](https://circleci.com/gh/mpscholten/typesafe-enum)

A lightweight, type safe enum library for PHP.

Use it to provide safe enums with autocompletion support. Fits well in domain driven designed applications.

### Get started

[](#get-started)

Install via composer

```
composer install mpscholten/typesafe-enum

```

### Basic Usage

[](#basic-usage)

```
class UserType extends \TypesafeEnum\Enum
{
    public static function PAID()
    {
        return new UserType('paid');
    }

    public static function FREE()
    {
        return new UserType('free');
    }

    public function isPaid()
    {
        return $this->is('paid');
    }

    public function isFree()
    {
        return $this->is('free');
    }
}

class User
{
    public function __construct($email, UserType $type)
    {
        $this->type = $type;
    }

    public function getType()
    {
        return $this->type;
    }
}

$user = new User('hello@example.com', UserType::PAID()); // Good
$user->getType()->isPaid(); // true
$user->getType()->isFree(); // false
(string) $user->getType(); // "paid"

$user = new User('hello@example.com', UserType::FREE()); // Good
$user->getType()->isFree(); // true
$user->getType()->isPaid(); // false
(string) $user->getType(); // "free"

$user = new User('hello@example.com', 'some string'); // Type error (PHP Catchable fatal error:  Argument 2 passed to User::__construct() must be an instance of UserType, integer given, called in ...)
$user = new User('hello@example.com', null); // Type error (PHP Catchable fatal error:  Argument 2 passed to User::__construct() must be an instance of UserType, null given, called in ...)
```

### Extended Usage

[](#extended-usage)

```
class Temperature extends Enum
{
    private $celsius;

    protected function __construct($value, $celsius)
    {
        parent::__construct($value);
        $this->celsius = $celsius;
    }

    public static function HOT()
    {
        return new Temperature('hot', 40);
    }

    public static function COLD()
    {
        return new Temperature('cold', 10);
    }

    public function isHot()
    {
        return $this->is('hot');
    }

    public function isCold()
    {
        return $this->is('cold');
    }

    /**
     * @return int
     */
    public function getCelsius()
    {
        return $this->celsius;
    }
}

$temperature = Temperature::HOT();
$temperature->isHot(); // true
$temperature->isCold(); // false
$temperature->getCelsius(); // 40
(string) $temperature; // "hot"

$temperature = Temperature::COLD();
$temperature->isCold(); // true
$temperature->isHot(); // false
$temperature->getCelsius(); // 10
(string) $temperature; // "cold"
```

As you can see above the suggested way to assign an attribute (in this case celsius) to each value in the value set of the enum is to add a new property to the class and to override the constructor. Don't use a `switch` statement for this.

This will make sure that there's always one assigned attribute per value in the value set of the enum, so the assigned attributes will not get out of sync with the value set.

### Usage with doctrine2

[](#usage-with-doctrine2)

You can use this library together with doctrine2 by using so called [embeddables](http://doctrine-orm.readthedocs.org/en/latest/tutorials/embeddables.html):

```
/** @Embeddable */
class UserType extends \TypesafeEnum\Enum
{
    /** @Column(type = "smallint", name = "type") */
    protected $value; // Override the `$value` property of \TypesafeEnum\Enum and apply mapping

    public static function PAID()
    {
        return new UserType(0);
    }

    public static function FREE()
    {
        return new UserType(1);
    }

    public function isPaid()
    {
        return $this->is(0);
    }

    public function isFree()
    {
        return $this->is(1);
    }
}

class User
{
    /** @Embedded(class = "UserType", columnPrefix = false) */
    private $type;

    public function __construct($email, UserType $type)
    {
        $this->type = $type;
    }
}
```

### Tests

[](#tests)

You can run the `phpunit` suite with `make`

```
make tests

```

### Contributing

[](#contributing)

Feel free to send pull requests!

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

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

3967d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/00ab5df48f62845d1e549e99c62eff4379d692bf8721f98dd4ee00275723692a?d=identicon)[mpscholten](/maintainers/mpscholten)

---

Top Contributors

[![mpscholten](https://avatars.githubusercontent.com/u/2072185?v=4)](https://github.com/mpscholten "mpscholten (9 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (5 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[yangbx/captcha-lumen

captcha for lumen

123.8k](/packages/yangbx-captcha-lumen)

PHPackages © 2026

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