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

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

intraworlds/enum
================

Lightweight implementation of enum value object

v4.1(2y ago)072.9k↓50%1MITPHPPHP &gt;=8.0

Since Nov 7Pushed 2y ago1 watchersCompare

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

READMEChangelog (10)Dependencies (3)Versions (11)Used By (0)

Lightweight implementation of enum value object in PHP.

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

[](#installation)

Use [Composer](https://getcomposer.org) to install the package:

```
composer require intraworlds/enum

```

Usage
-----

[](#usage)

Extend `IW\Enum` and add arbitrary constants which becomes your enum keys.

```
final class Hash extends Enum
{
  const MD5 = 'md5';
  const SHA1 = 'sha1';
}
```

Obtain instance of enum value by calling static function `::()`.

```
$md5 = Hash::MD5();
```

Main advantage of use this library is of cource type hinting. Therefore given instance is always valid in principle of DDD value object.

```
function crack(Hash $hash) {
  echo 'cracking ... ' . $hash; // notice that enum is implementing __toString() method
}

crack(Hash::SHA1()); // cracking ... sha1
crack(Hash::SHA1);   // throws TypeError
```

The function is returning a singleton so you can compare it with `===`

```
var_dump($md5 === MD5()); // true
var_dump($md5 === SHA1()); // false
```

Actually you must use `===` for strict comparison. With `==` [loose comparison](http://php.net/manual/en/types.comparisons.php#types.comparisions-loose) PHP compares only that objects are the same class.

```
var_dump($md5 == MD5()); // true
var_dump($md5 == SHA1()); // true - DON'T use == comparison!
```

Access actual value with method `getValue()`

```
var_dump($md5->getValue() === Hash::MD5); // true
```

Switch statements must be defined by values because of [loose comparison](http://php.net/manual/en/types.comparisons.php#types.comparisions-loose).

```
switch ($hash->getValue()) {
  case Hash::MD5: ...
  case Hash::SHA1: ...
}
```

You can user method `search()` for obtaining instance of enum by a value.

```
$contentType = ContentTypeEnum::search(apache_request_headers()['Content-Type']);
```

PHPStan integration
-------------------

[](#phpstan-integration)

This library is distributed with PHPStan extension which's providing magic methods reflection. Just add following configuration to your `phpstan.neon`

```
services:
    -
        class: IW\PHPStan\Reflection\EnumMethodsReflectionExtension
        tags:
            - phpstan.broker.methodsClassReflectionExtension
```

Alternatives
------------

[](#alternatives)

- we took most of the inspiration from library [myclabs/php-enum](https://packagist.org/packages/myclabs/php-enum). It's nice implementation but it's missing singletons and it's not optimized for PHP7.
- PHP's [SplEnum](https://secure.php.net/manual/en/class.splenum.php) needs an extension and also does not support singletons.

License
-------

[](#license)

All contents of this package are licensed under the [MIT license](LICENSE).

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity72

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

Recently: every ~364 days

Total

10

Last Release

892d ago

Major Versions

v2.3.1 → v3.02020-06-04

v3.0.1 → v4.02022-03-23

PHP version history (2 changes)v2.0PHP &gt;=7.1

v4.0PHP &gt;=8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2031969?v=4)[Ondřej Ešler](/maintainers/esler)[@esler](https://github.com/esler)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[maciejczyzewski/bottomline

A full-on PHP manipulation utility belt that provides support for working with arrays, objects, and iterables; a lodash or underscore equivalent for PHP.

477631.4k10](/packages/maciejczyzewski-bottomline)[spatie/laravel-pjax

A pjax middleware for Laravel 5

513371.8k11](/packages/spatie-laravel-pjax)[phospr/fraction

A composer-installable fractions library

34327.5k2](/packages/phospr-fraction)

PHPackages © 2026

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