PHPackages                             webdevcave/enum-index-accessor - 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. webdevcave/enum-index-accessor

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

webdevcave/enum-index-accessor
==============================

Dynamic index accessing made simple

v1.0(2y ago)1171↓50%MITPHPPHP &gt;=8.1

Since Apr 24Pushed 2y ago1 watchersCompare

[ Source](https://github.com/WebdevCave/enum-index-accessor-php)[ Packagist](https://packagist.org/packages/webdevcave/enum-index-accessor)[ RSS](/packages/webdevcave-enum-index-accessor/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

enum-index-accessor
===================

[](#enum-index-accessor)

Install with composer:
----------------------

[](#install-with-composer)

```
composer install webdevcave/enum-index-accessor

```

Why?
----

[](#why)

From php 8.1 or newer, the possibility organized enumerators was made real. It works just fine until we have to read those values dynamically. Here is an example:

```
enum HexColors: string {
    case RED = '#FF0000';
    case GREEN = '#00FF00';
    case BLUE = '#0000FF';
    case WHITE = '#FFFFFF';
    case BLACK = '#000000';
    // and so on...
}

$index = 'BLUE'; // Imagine this is a dynamic value
$constant = HexColors::class."::$index"; // In a real-world application HexColors
                                         // will most probably be declared under a namespace
$color = null;

// Before we proceed, we have to ensure the specified index exists or our code will break
if (defined($constant)) {
    $color = constant($constant)->value; // Now we read its value
}

// You will probably want to assign a default value in case something went wrong
if (is_null($color)) {
    $color = HexColors::RED->value;
}

// Now we can finally proceed with our task...
```

Does this looks too verbose and/or messy for your taste? Now imagine if your application have to read multiple values like this.

That's why we created this package! Now you can do the same task this way:

```
$color = HexColors::tryValue($index) ?? HexColors::value('RED');
```

How?

```
use \WebdevCave\EnumIndexAccessor\BackedEnumIndexAccessor; // step 1: Import the trait

enum HexColors: string {
    case RED = '#FF0000';
    case GREEN = '#00FF00';
    case BLUE = '#0000FF';
    case WHITE = '#FFFFFF';
    case BLACK = '#000000';
    // and so on...

    use BackedEnumIndexAccessor; // step 2: use it
}
```

~ And voila! Magic 🪄!

---

Other use cases...
------------------

[](#other-use-cases)

We followed the php team standards for naming the methods for ease of use. Here is a list of all of them:

```
HexColors::hasIndex($index); // Checks if a case statement was set in the enumerator (boolean)
HexColors::index($index); // Read the object from given index (skips index check)
HexColors::tryIndex($index); // Read the object from given index (null on non-existent)
HexColors::value($index); // Read the value from given index (skips index check)
HexColors::tryValue($index); // Read the value from given index (null on non-existent)
```

For pure enumerators (without backing values), use the pure enumerator trait as follows:

```
use \WebdevCave\EnumIndexAccessor\PureEnumIndexAccessor; // step 1: Import the trait

enum Fruits {
    case ORANGE;
    case PEAR;
    case APPLE;
    // and so on...

    use PureEnumIndexAccessor; // step 2: use it
}
```

**Important note:** The methods `value` and `tryValue` are not available as pure enumerators doesn't carry any values on them

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

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

Unknown

Total

1

Last Release

755d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5069326?v=4)[Carlos Alberto Bertholdo Carucce](/maintainers/carloscarucce)[@carloscarucce](https://github.com/carloscarucce)

---

Top Contributors

[![carloscarucce](https://avatars.githubusercontent.com/u/5069326?v=4)](https://github.com/carloscarucce "carloscarucce (6 commits)")

---

Tags

accessorenumenumsextensionphpphp-libraryphp8enumenumeratorextensionaccessor

### Embed Badge

![Health badge](/badges/webdevcave-enum-index-accessor/health.svg)

```
[![Health](https://phpackages.com/badges/webdevcave-enum-index-accessor/health.svg)](https://phpackages.com/packages/webdevcave-enum-index-accessor)
```

###  Alternatives

[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49644.8M97](/packages/marc-mabe-php-enum)[ducks-project/spl-types

Polyfill Module for SplType PHP extension. This extension aims at helping people making PHP a stronger typed language and can be a good alternative to scalar type hinting. It provides different typehandling classes as such as integer, float, bool, enum and string

1032.4k](/packages/ducks-project-spl-types)[richardfan1126/yii2-js-register

Yii2 widget to register JS into view

1357.2k7](/packages/richardfan1126-yii2-js-register)

PHPackages © 2026

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