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

ActiveLibrary

mesavolt/named-enum
===================

Named enums for PHP 8.1+

v0.8.5(1y ago)311.4k↓22.6%[2 PRs](https://github.com/MesaVolt/NamedEnum/pulls)MITPHPPHP &gt;=8.1CI failing

Since May 15Pushed 1y ago2 watchersCompare

[ Source](https://github.com/MesaVolt/NamedEnum)[ Packagist](https://packagist.org/packages/mesavolt/named-enum)[ RSS](/packages/mesavolt-named-enum/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (20)Used By (0)

Named enum for PHP 7.1 +
========================

[](#named-enum-for-php-71-)

[![Latest Stable Version](https://camo.githubusercontent.com/b47895d8363549562726a8069ffcfe6c53cef54780b7db6d466d94d5d770a23b/68747470733a2f2f706f7365722e707567782e6f72672f6d657361766f6c742f6e616d65642d656e756d2f762f737461626c65)](https://packagist.org/packages/mesavolt/named-enum)[![Build Status](https://camo.githubusercontent.com/fe3f4589c8c14414ac695f7f3d2094c0720f184811863d4db959bb0071ccd108/68747470733a2f2f7472617669732d63692e6f72672f4d657361566f6c742f4e616d6564456e756d2e737667)](https://travis-ci.org/MesaVolt/NamedEnum)[![Coverage Status](https://camo.githubusercontent.com/566383e110e91c2d4c759bb31ba8046dc30f7d2d2048a24296253245a3fcee0f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4d657361566f6c742f4e616d6564456e756d2f62616467652e737667)](https://coveralls.io/github/MesaVolt/NamedEnum)[![License](https://camo.githubusercontent.com/08bf5572413f4950ccebb0f98b4517273c9e93439e893f735b51cc6e35ab82d0/68747470733a2f2f706f7365722e707567782e6f72672f6d657361766f6c742f6e616d65642d656e756d2f6c6963656e7365)](https://packagist.org/packages/mesavolt/named-enum)

Usage
-----

[](#usage)

Add the package to your project :

```
composer require mesavolt/named-enum
```

Define a class that extends `Mesavolt\Enum\NamedEnum` with your enum values and names. The values should be defined as class constants (their visibility is up to you) and the names should be declared in a protected static class variable `$VALUE_NAMES`, indexed by the corresponding value.

> Quick note: By default, `NamedEnum::getName($value)` silently ignores undefined values and returns `null` for any undefined value. You can extend `Mesavolt\Enum\StrictNamedEnum`for a strict check that will throw an `\InvalidArgumentException` if you pass an undefined enum value to `getName($value)`.

```
