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

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

petrknap/enum
=============

Enumerated type for PHP

v3.0.1(2y ago)04LGPL-3.0-or-laterDockerfilePHP &gt;=8.0

Since Jan 23Pushed 2y ago1 watchersCompare

[ Source](https://github.com/petrknap/php-enum)[ Packagist](https://packagist.org/packages/petrknap/enum)[ Docs](https://github.com/petrknap/php-enum)[ Fund](https://petrknap.github.io/donate.html)[ RSS](/packages/petrknap-enum/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (17)Used By (0)

Enumerated type for PHP
=======================

[](#enumerated-type-for-php)

- [What is Enum?](#what-is-enum)
- [Why use Enums instead of Constants?](#why-use-enums-instead-of-constants)
- [Usage of php-enum](#usage-of-php-enum)
    - [Enum declaration](#enum-declaration)
    - [Enum usage](#enum-usage)
    - [Tips &amp; Tricks](#tips--tricks)
- [How to install](#how-to-install)

What is Enum?
-------------

[](#what-is-enum)

> In computer programming, an **enumerated type** (also called **enumeration** or **enum**, or **factor** in the R programming language, and a **categorical variable** in statistics) is a data type consisting of a set of named values called **elements**, **members**, **enumeral**, or **enumerators** of the type. The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an *enumerated type has values that are different from each other*, and that can be compared and assigned, but which are not specified by the programmer as having any particular concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily. -- [Enumerated type - Wikipedia, The Free Encyclopedia](https://en.wikipedia.org/w/index.php?title=Enumerated_type&oldid=701057934)

Why use Enums instead of Constants?
-----------------------------------

[](#why-use-enums-instead-of-constants)

Because **it is safer and less scary** than using constants. Don't trust me? Let see at this code:

```
use PetrKnap\Enum\Readme\MyBoolean;

$isTrue = function (int $myBoolean)
{
    switch($myBoolean) {
        case MyBoolean::MY_TRUE:
            return true;
        case MyBoolean::MY_FALSE:
            return false;
    }
};

var_dump($isTrue(MyBoolean::MY_TRUE));  // true - correct
var_dump($isTrue(MyBoolean::MY_FALSE)); // false - correct
var_dump($isTrue(0));                   // none
var_dump($isTrue(1));                   // true - expected
var_dump($isTrue(2));                   // false
var_dump($isTrue((int) true));          // true - expected
var_dump($isTrue((int) false));         // none
```

And now the **same code [with enum](https://www.php.net/manual/en/language.types.enumerations.php)** instead of constants:

```
use PetrKnap\Enum\Readme\MyBoolean;

$isTrue = function (MyBoolean $myBoolean): bool
{
    switch($myBoolean) {
        case MyBoolean::MyTrue:
            return true;
        case MyBoolean::MyFalse:
            return false;
    }
};

var_dump($isTrue(MyBoolean::MyTrue));  // true - correct
var_dump($isTrue(MyBoolean::MyFalse)); // false - correct
```

---

Run `composer require petrknap/enum` to install it. You can [support this project via donation](https://petrknap.github.io/donate.html). The project is licensed under [the terms of the `LGPL-3.0-or-later`](./COPYING.LESSER).

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity76

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

Recently: every ~673 days

Total

14

Last Release

872d ago

Major Versions

v0.4 → v1.02016-01-24

v1.1.1 → v2.0.02016-08-10

v2.1.0 → v3.0.02023-10-17

PHP version history (4 changes)v0.1PHP &gt;=5.4

v1.0.2PHP &gt;=5.3.3

v2.1.0PHP &gt;=5.5

v3.0.0PHP &gt;=8.0

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

enumenumeratedenumerated-typesphpphp-libraryenumenumeratedenumerated-types

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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