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

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

eclipxe/enum
============

Enum based on the Brent Roose enum idea https://stitcher.io/blog/php-enums

v0.2.8(2mo ago)3184.2k↑11.3%6MITPHPPHP &gt;=7.2CI passing

Since Mar 25Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/eclipxe13/enum)[ Packagist](https://packagist.org/packages/eclipxe/enum)[ Docs](https://github.com/eclipxe13/enum)[ RSS](/packages/eclipxe-enum/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (2)Versions (11)Used By (6)

`eclipxe/enum`
==============

[](#eclipxeenum)

[![Source Code](https://camo.githubusercontent.com/8d658cc4d00375125ee883217d6066214c14b1e88e318eda3328c1fe0431111f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d65636c6970786531332f656e756d2d626c75653f7374796c653d666c61742d737175617265)](https://github.com/eclipxe13/enum)[![Packagist PHP Version Support](https://camo.githubusercontent.com/1070b29e968fb8a73ed8e33d2f38293fce78ef9c2eee88e92c5ebb63e01842e7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f65636c697078652f656e756d3f7374796c653d666c61742d737175617265)](https://packagist.org/packages/eclipxe/enum)[![Latest Version](https://camo.githubusercontent.com/1a64d690d40be43386127298b713164992c13908c7b782d592ba6d8e88101cb4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f65636c6970786531332f656e756d3f7374796c653d666c61742d737175617265)](https://github.com/eclipxe13/enum/releases)[![Software License](https://camo.githubusercontent.com/361c5abd62790a466fe5f847dedbc870ec90f282c9215ea00567e06c1634dfba/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f65636c6970786531332f656e756d3f7374796c653d666c61742d737175617265)](https://github.com/eclipxe13/enum/blob/main/LICENSE)[![Build Status](https://camo.githubusercontent.com/33affebe5417cc89cbf59f53b4df9646377785fdf0b31b788a4a8d82e32151cd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f65636c6970786531332f656e756d2f6275696c642e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265)](https://github.com/eclipxe13/enum/actions/workflows/build.yml?query=branch:main)[![Scrutinizer](https://camo.githubusercontent.com/e0351d7badcf0d7a1266249ef48d68a8eb69fe1a8d4c6ea8b6789a2d3be57915/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f65636c6970786531332f656e756d2f6d61696e3f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/eclipxe13/enum/)[![Coverage Status](https://camo.githubusercontent.com/3e5792d5fa0be8fb6cd42e29188146e7be6d54d9366db35277386804574d3cee/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f65636c6970786531332f656e756d2f6d61696e3f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/eclipxe13/enum/code-structure/main/code-coverage)[![Total Downloads](https://camo.githubusercontent.com/7dde56a834bccd497e49a82cb91f3b195a1527802e1c4c9efe185f687bb9a4d7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f65636c697078652f656e756d3f7374796c653d666c61742d737175617265)](https://packagist.org/packages/eclipxe/enum)

> Enum based on the Brent Roose enum idea

After reading the article [PHP Enums from Brent Roose](https://stitcher.io/blog/php-enums) and review the implementation made on [spatie/enum](https://github.com/spatie/enum) I think that it overloaded my expectations. Maybe spatie/enum version 1.0 was more close to what I needed.

So, I created this framework-agnostic implementation library about the same concept.

As of PHP 8.1 enums are part of the language, it means that this library will not be required anymore. Read the PHP documentation  and adapt your code. One big difference between PHP enums and the objects on this library is that native PHP enums cannot use the magic method `__toString` (they are not *`Stringable`*), and the enums on this library are.

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

[](#installation)

Use [composer](https://getcomposer.org/), install using:

```
composer require eclipxe/enum
```

Usage
-----

[](#usage)

*Enum* in other languages are `TEXT` for code, `INTEGER` for values.

There are two meaningful information: *index* (`integer`) and *value* (`string`).

This library provides `Eclipxe\Enum` *abstract class* to be extended. The *value* is the method's *name* as declared in docblock. The *index* is the position (starting at zero) in the docblock.

Values are registered one by one taking the overridden value, or the method's name.

Indices are registered one by one taking the overridden index, or the maximum registered value plus 1.

### Enum example

[](#enum-example)

```
