PHPackages                             dnl-blkv/simple-php-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. dnl-blkv/simple-php-enum

ActiveLibrary

dnl-blkv/simple-php-enum
========================

A simple C/C++ alike enum library.

0.6.2(9y ago)2231MITPHPPHP &gt;=7.0

Since Mar 30Pushed 9y ago1 watchersCompare

[ Source](https://github.com/dnl-blkv/simple-php-enum)[ Packagist](https://packagist.org/packages/dnl-blkv/simple-php-enum)[ Docs](https://github.com/dnl-blkv/simple-php-enum)[ RSS](/packages/dnl-blkv-simple-php-enum/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (17)Used By (0)

Simple PHP Enum
===============

[](#simple-php-enum)

Description
-----------

[](#description)

A simple C/C++ alike PHP library for Enums.

Stats and Health
----------------

[](#stats-and-health)

[![Travis CI](https://camo.githubusercontent.com/9afc4a87bc602eec8c2c9f108fc3c36c7f2b4be26ed56fcff123838ed16d20b9/68747470733a2f2f7472617669732d63692e6f72672f646e6c2d626c6b762f73696d706c652d7068702d656e756d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/dnl-blkv/simple-php-enum)[![codecov](https://camo.githubusercontent.com/5a903c0427f227247bbb712e56eff16cb7a1144b8d7c52d111ccd894fc526b8f/68747470733a2f2f636f6465636f762e696f2f67682f646e6c2d626c6b762f73696d706c652d7068702d656e756d2f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/dnl-blkv/simple-php-enum)[![Latest Stable Version](https://camo.githubusercontent.com/9a9bcbb990722b20c0f345e35828e65ae3f324046c82b073ab5b682a2c4446d2/68747470733a2f2f706f7365722e707567782e6f72672f646e6c2d626c6b762f73696d706c652d7068702d656e756d2f762f737461626c65)](https://packagist.org/packages/dnl-blkv/simple-php-enum)[![License](https://camo.githubusercontent.com/f0b28bb1e072d1695c922efe4804233621f5b66738c031041cee835d38c0b482/68747470733a2f2f706f7365722e707567782e6f72672f646e6c2d626c6b762f73696d706c652d7068702d656e756d2f6c6963656e7365)](https://packagist.org/packages/dnl-blkv/simple-php-enum)

PHP Version Support
-------------------

[](#php-version-support)

The package is new and thus only supports PHP `>=7.0`.

How To Basic
============

[](#how-to-basic)

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

[](#installation)

1. [Install composer](https://getcomposer.org/doc/00-intro.md)
2. Open your project folder in terminal
3. Enter `composer require dnl-blkv/simple-php-enum`
4. Wait for the composer to finish the job
5. Now, you can start using the Simple PHP Enums as described below!

Defining Enums
--------------

[](#defining-enums)

Defining a basic Enum with the package is straightforward:

```
use dnl_blkv\enum\AbstractEnum;

/**
 * @method static static CAT()
 * @method static static DOG()
 * @method static static BIRD()
 * @method static static FISH()
 */
class AnimalEnum extends AbstractEnum
{
    const CAT = null;
    const DOG = null;
    const BIRD = null;
    const FISH = null;
}

```

Here `null` means auto-determined ordinal value, or *auto-ordinal*. The default auto-ordinal is `0`. The further auto-ordinal values are determined as `{previous ordinal} + 1`.

Enum constant names MUST be [PSR-1-compliant](http://www.php-fig.org/psr/psr-1/) AND start from a capital letter. If a constant name does not conform with the rules, the constant is ignored.

Creating
--------

[](#creating)

Once the class is defined, the enums can be acquired as:

```
$animal = AnimalEnum::CAT();

```

Or:

```
$animal = AnimalEnum::getByName('CAT');

```

Or:

```
$animal = AnimalEnum::getByOrdinal(0);

```

In the examples above, if the name or the ordinal is not defined, exceptions will be thrown (`UndefinedEnumNameException` and `UndefinedEnumOrdinalException` correspondingly).

Accessing
---------

[](#accessing)

You can access the name (string representation) and the ordinal (numeric representation) of the enum:

```
echo $animal->getName() . ' ' . $animal->getOrdinal(); // Outputs "CAT 0"

```

Comparison
----------

[](#comparison)

### Equality

[](#equality)

The enums can be checked for equality as such:

```
$cat = AnimalEnum::CAT();
$otherCat = AnimalEnum::CAT();
$dog = AnimalEnum::DOG();
var_dump($cat->isEqual($otherCat)) // Outputs "bool(true)"
var_dump($cat->isEqual($dog)) // Outputs "bool(false)"

```

Intuitively, two enums of different types are never equal. If we have an enum of type `SomeOtherEnum` with `const VALUE = 0;` then the following holds:

```
var_dump(SomeOtherEnum::VALUE()->isEqual(AnimalEnum::CAT())) // Outputs "bool(false)"

```

### Comparison by Ordinal

[](#comparison-by-ordinal)

It is also possible to compare the Simple PHP Enums by their ordinal values. There are four methods defined for this, as shown below:

```
/**
 * @method static static READ()
 * @method static static WRITE()
 * @method static static ADMIN()
 */
class AccessLevelEnum extends Enum
{
    const READ = null;
    const WRITE = null;
    const ADMIN = null;
}

var_dump(AccessLevelEnum::READ()->isLess(AccessLevelEnum::WRITE())) . PHP_EOL // -> "bool(true)"
var_dump(AccessLevelEnum::READ()->isGreater(AccessLevelEnum::WRITE())) . PHP_EOL // -> "bool(false)"
var_dump(AccessLevelEnum::READ()->isGreaterOrEqual(AccessLevelEnum::READ())) . PHP_EOL // -> "bool(true)"
var_dump(AccessLevelEnum::READ()->isLessOrEqual(AccessLevelEnum::ADMIN())) . PHP_EOL // -> "bool(true)"

```

If two enums of different types are compared, the `InvalidArgumentException` is thrown.

How To Advanced
===============

[](#how-to-advanced)

Defining Enums with Custom Ordinals
-----------------------------------

[](#defining-enums-with-custom-ordinals)

Besides letting the library assign the ordinals automatically, you could manually assign custom integer values to the ordinals:

```
use dnl_blkv\enum\AbstractEnum;

/**
 * @method static static PIZZA()
 * @method static static SUSHI()
 * @method static static KEBAB()
 * @method static static SALAD()
 */
class FoodEnum extends AbstractEnum
{
    const PIZZA = 5;
    const SUSHI = null;
    const KEBAB = 8;
    const SALAD = 10;
}

```

In this case the enums will be defined as following:

```
echo FoodEnum::PIZZA()->getOrdinal() . PHP_EOL; // Outputs "5"
echo FoodEnum::SUSHI()->getOrdinal() . PHP_EOL; // Outputs "6"
echo FoodEnum::KEBAB()->getOrdinal() . PHP_EOL; // Outputs "8"
echo FoodEnum::SALAD()->getOrdinal() . PHP_EOL; // Outputs "10"

```

Duplicate Ordinals
------------------

[](#duplicate-ordinals)

Similarly to the vanilla C/C++ enums, this Simple PHP Enums allow for duplicate ordinals. This may be used for tackling such cases as a default value:

```
use dnl_blkv\enum\AbstractEnum;

/**
 * @method static static LAGER()
 * @method static static IPA()
 * @method static static PORTER()
 * @method static static STOUT()
 * @method static static DEFAULT()
 * @method static static AFTER_DEFAULT()
 */
class BeerEnum extends AbstractEnum
{
    const LAGER = 0;
    const IPA = null;
    const PORTER = null;
    const STOUT = null;
    const DEFAULT = 0;
    const AFTER_DEFAULT = null;
}

```

For the enum defined above, the following will hold:

```
echo BeerEnum::DEFAULT()->getOrdinal() . PHP_EOL; // Outputs "0"
echo BeerEnum::AFTER_DEFAULT()->getOrdinal() . PHP_EOL; // Outputs "1"

```

If you are getting an enum with duplicate ordinal using a magic method or by name, it works as usual.

```
echo BeerEnum::DEFAULT()->getName() . PHP_EOL; // Outputs "DEFAULT"
echo BeerEnum::getByName('DEFAULT')->getName() . PHP_EOL; // Outputs "DEFAULT"

```

However, if you get it by an ordinal, the behavior is slightly different, and you have two options as shown below:

```
echo BeerEnum::getFirstByOrdinal(0)->getName() . PHP_EOL; // Outputs "LAGER"
$allEnumWithOrdinalZero = BeerEnum::getAllByOrdinal(0);
echo $allEnumWithOrdinalZero[0]->getName() . PHP_EOL; // Outputs "LAGER"
echo $allEnumWithOrdinalZero[1]->getName() . PHP_EOL; // Outputs "DEFAULT"

```

More Equality
-------------

[](#more-equality)

The Simple PHP Enum library only creates each enum object once and then reuses it. Therefore, the enums are comparable with `===` or its alias `isSame`. This kind comparison is stricter than `isEqual`. Whereas `isEqual` only accounts for the enum type and ordinal, `isSame` also takes the `name` into account:

```
var_dump(BeerEnum::LAGER()->isEqual(BeerEnum::LAGER())); // Outputs "bool(true)"
var_dump(BeerEnum::LAGER()->isEqual(BeerEnum::DEFAULT())); // Outputs "bool(true)"
var_dump(BeerEnum::LAGER() === BeerEnum::LAGER()); // Outputs "bool(true)"
var_dump(BeerEnum::LAGER() === BeerEnum::DEFAULT()); // Outputs "bool(false)"
var_dump(BeerEnum::LAGER()->isSame(BeerEnum::LAGER())); // Outputs "bool(true)"
var_dump(BeerEnum::LAGER()->isSame(BeerEnum::DEFAULT())); // Outputs "bool(false)"

```

Checking Existence of Names and Ordinals
----------------------------------------

[](#checking-existence-of-names-and-ordinals)

If you wish to check whether or not certain enum type has a given name or ordinal, there are methods allowing you to easily do so:

```
var_dump(BeerEnum::isNameDefined('STOUT')) // Outputs "bool(true)";
var_dump(BeerEnum::isNameDefined('VODKA')) // Outputs "bool(false)";
var_dump(BeerEnum::isOrdinalDefined(3)) // Outputs "bool(true)";
var_dump(BeerEnum::isOrdinalDefined(420)) // Outputs "bool(false)";

```

Converting to String
--------------------

[](#converting-to-string)

The enums have an embedded magical mechanism for serialization:

```
echo BeerEnum::IPA() . PHP_EOL;

/*
 * Outputs:
 * {
 *     "\your\name\space\BeerEnum": {
 *         "name": "IPA",
 *         "ordinal": 1
 *      }
 * }
 */

```

Notes
=====

[](#notes)

Extension
---------

[](#extension)

All the internals of the `AbstractEnum` class are either `public` or `protected`. Therefore, it is completely open for extension and allows you to build your own, more complex constructions on top of it.

Use with Databases
------------------

[](#use-with-databases)

If you opt to use these enums with databases and store the ordinals, I would recommend to make sure that no stored enum has duplicate ordinals. Otherwise, it could happen that you store `DEFAULT = 0`, but receive `IPA = 0` upon recreation.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity56

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

Total

16

Last Release

3318d ago

### Community

---

Top Contributors

[![dnl-blkv](https://avatars.githubusercontent.com/u/6583731?v=4)](https://github.com/dnl-blkv "dnl-blkv (17 commits)")

---

Tags

cc-plus-plusenumenumerated-typesenumerationphpphp-enumphp7php71simple

### Embed Badge

![Health badge](/badges/dnl-blkv-simple-php-enum/health.svg)

```
[![Health](https://phpackages.com/badges/dnl-blkv-simple-php-enum/health.svg)](https://phpackages.com/packages/dnl-blkv-simple-php-enum)
```

PHPackages © 2026

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