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

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

nxtlvlsoftware/enums
====================

Brings enumerations to PHP!

11PHP

Since Mar 2Pushed 7y ago1 watchersCompare

[ Source](https://github.com/NxtLvLSoftware/php-enums)[ Packagist](https://packagist.org/packages/nxtlvlsoftware/enums)[ RSS](/packages/nxtlvlsoftware-enums/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Enumerations for PHP
====================

[](#enumerations-for-php)

*Brings class-based enums to PHP!*

[![Build Status](https://camo.githubusercontent.com/a3a4852c2c38a212d21f3318a70068f27ea9f5e818ba661c893749206bf2575f/68747470733a2f2f7472617669732d63692e6f72672f4e78744c764c536f6674776172652f7068702d656e756d732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/NxtLvLSoftware/php-enums)

### About

[](#about)

This package provides you with a quick and easy way to implement enumerations in your PHP codebase. The purpose of this package is to make defining enums quick and do it with as least code as possible. This is achieved by reading constants from a class providing and then providing access to them through magic static methods that have the same name as the constant. We use this approach to wrap the underlying constant value inside an immutable object that remains the same throughout the current runtime. Using an object rather than the underlying value of the constant helps to replicate the behaviour of enums in other programming languages (an enum is not treated as an int even through deep down it is) and aims to deter the reliance on 'magic numbers' (constant values which rarely change but are not guaranteed to remain the same) by hiding the underlying value.

Here's a quick example:

```
final class MyEnum extends nxtlvlsoftware\enums\Enum {
    protected const ENUM_INT = 0;
    protected const ENUM_STRING = "string";
}
```

We can then access these declared enums by calling a static method on the class with the equivalent name:

```
MyEnum::ENUM_INT();
MyEnum::ENUM_STRING();
```

We use protected constants in this example to hide them from other classes, we only want them accessed through the static method. The enum class is also declared as final to stop other other classes extending it.

### Installation

[](#installation)

All you have to do to install with composer is the following:

```
$ composer require nxtlvlsoftware/enums
```

Or add it directly to your composer.json manifest:

```
{
    "require": {
        "nxtlvlsoftware/enums": "*"
    }
}
```

The library will start working out of the box, just make sure you extend the base enum class or the static methods won't be defined on your class.

### IDE Auto-completion

[](#ide-auto-completion)

You may be thinking to yourself `Cool, but my IDE won't know these 'enums' even exist!` and you'd be right. An IDE such as PhpStorm will have no idea these magic methods exist which is why we provide an 'IDE helper generator'. It's a simple CLI application that will scan a directory for enum classes and generate a file [like this](https://gist.github.com/JackNoordhuis/a73dbf5cd32dac4ce44ad9177add3816)that will tell your IDE that these methods actually will exist when the code runs.

If you require this package globally with composer the command will be available anywhere on your machine as `enums` or any project which has this package as a dependency will link the script into the projects bin directory (`vendor/bin/enums` by default).

You will have to define at least one directory to scan and the location and filename of the stubs:

```
$ php vendor/bin/enums generate --dir /Users/Jack/Projects/MyProject/src --dir /Users/Jack/Projects/MyProject/vendor --out /Users/Jack/Projects/MyProject/enum_stubs.php
```

This command will scan the source directory of the project and all of it's installed composer dependencies and generate the IDE helper stubs in the `enum_stubs.php` file.

You can use the shortcut option names `-d` for specifying a directory and `-o` for the output file instead of the full names. Creating a simple script or PhpStorm configuration to quickly run this command will probably improve your workflow as typing the whole command out every time you update one of your enum classes will get very tiresome.

### Under the hood

[](#under-the-hood)

The first time an enum is accessed on a class the library uses reflection to fetch all the constants defined, it then stores a new instance of the class (in an array) which internally stores the enum value and name. This allows us to implement the magic to string method to display the name of the enum for debugging purposes. The value is stored for potential a future feature that will allow an underlying value to be returned in place of the enum object for special use-cases.

### Issues

[](#issues)

Found a problem with this library? Make sure to open an issue on the [issue tracker](https://github.com/NxtLvLSoftware/php-enums/issues) and we'll get it sorted!

**The content of this repo is licensed under the Unlicense. A full copy of the license is available [here](LICENSE).**

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10399774?v=4)[Jack Noordhuis](/maintainers/JackNoordhuis)[@JackNoordhuis](https://github.com/JackNoordhuis)

---

Top Contributors

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

### Embed Badge

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

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

###  Alternatives

[whitemerry/phpkin

PHP Zipkin implementation

7826.5k3](/packages/whitemerry-phpkin)[overtrue/laravel-options

Global options module for Laravel application.

5218.5k3](/packages/overtrue-laravel-options)[codefog/contao-polls

polls extension for Contao Open Source CMS

152.6k](/packages/codefog-contao-polls)

PHPackages © 2026

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