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

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

vijinho/enums
=============

PHP7 Enum Implementation

1.0.2(8y ago)723.5k↓33.3%GPL-3.0PHPPHP &gt;=5.6

Since Aug 26Pushed 4y ago1 watchersCompare

[ Source](https://github.com/vijinho/php7-enums)[ Packagist](https://packagist.org/packages/vijinho/enums)[ Docs](https://github.com/vijinho/enums)[ RSS](/packages/vijinho-enums/feed)WikiDiscussions dev-master Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

PHP7 Enums
==========

[](#php7-enums)

[![Travis CI](https://camo.githubusercontent.com/d1778012d3eb84fa6cd75cba3f3c6ed1a84ca4ee05ed55ef618c7b6268898d99/68747470733a2f2f7472617669732d63692e6f72672f76696a696e686f2f706870372d656e756d732e7376673f6272616e63683d6465762d6d6173746572)](https://travis-ci.org/vijinho/php7-enums)[![Build Status](https://camo.githubusercontent.com/8c2ca1ee16de4841a7e7fa62c7fbff37a773cfa4f702060d2323efae75865bf9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f76696a696e686f2f706870372d656e756d732f6261646765732f6275696c642e706e673f623d6465762d6d6173746572)](https://scrutinizer-ci.com/g/vijinho/php7-enums/build-status/dev-master)[![Code Coverage](https://camo.githubusercontent.com/66be079996b757247f074500418c72d9eb99bb6a9b4d93d003074877a15ecfd7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f76696a696e686f2f706870372d656e756d732f6261646765732f636f7665726167652e706e673f623d6465762d6d6173746572)](https://scrutinizer-ci.com/g/vijinho/php7-enums/?branch=dev-master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b744def0accd6250976ac96823464c55a074f6ea8d975dd07fa5d8f2822d8557/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f76696a696e686f2f706870372d656e756d732f6261646765732f7175616c6974792d73636f72652e706e673f623d6465762d6d6173746572)](https://scrutinizer-ci.com/g/vijinho/php7-enums/?branch=dev-master)

This is an implementation for ENUM types in PHP7 which lives on github at [vijinho/php7-enums](https://github.com/vijinho/php7-enums). It's a little different from the other implementations I saw which didn't quite fit my needs and it uses quite a bit of PHP7 [magic](http://php.net/manual/en/language.oop5.magic.php) and [overloading](http://php.net/manual/en/language.oop5.overloading.php) to achieve the results I wanted.

Quick Start
-----------

[](#quick-start)

### Real-world example

[](#real-world-example)

```
class Storage extends Enum
{
    protected static $caseSensitive = true;
    protected static $capitalize = true;

    protected static $values = [
        'BIT' => 1,
        'BYTE' => 8,
        'KILOBYTE' => 8 * 1024,
        'GIGABYTE' => 8 * 1024 * 1024 * 1024,
        'TERABYTE' => 8 * 1024 * 1024 * 1024 * 1024,
    ];
}

// add definition of a megabyte in bits
$s = new Storage;
$s(['MEGABYTE' => 1024 * Storage::KILOBYTE()]);
echo $s;

// get definition of 8 bits
$name = $s->key(8);
echo $name; // BYTE

echo $s->GIGABYTE; // 8589934592
echo $s::KILOBYTE(); // 8192
echo $s->value('TERABYTE'); // 8796093022208
echo Storage::value('BYTE'); // 8
echo Storage::BYTE(); // 8
```

### Using Enum without extending it

[](#using-enum-without-extending-it)

I do not advise you to do this because we're using static class members.

```
use vijinho\Enums\Enum;

$e = new Enum(); // new empty enum
$e([
    'mercedes' => 'luxury',
    'ferrari' => 'sports',
    'BMW'
]);
echo $e; // outputs to JSON serialized string by magic!

/*
{
    "mercedes": "luxury",
    "ferrari": "sports",
    "BMW": "BMW"
}
*/

$e->add(['BMW' => 'Bob Marley & The Wailers']); // cannot override existing value
echo $e->value('BMW'); // BMW
$e->capitalize(true);
$e->add('Audi');
echo $e;

/*
{
    "MERCEDES": "luxury",
    "FERRARI": "sports",
    "BMW": "BMW",
    "AUDI": "Audi"
}
*/

echo $e->MERCEDES; // luxury
echo $e->FERRARI(); // sports
echo Enum::AUDI(); // Audi

// add non-string value (array)
echo "Example 17\n";
$e(['trabant' => ['Germany', 'Eastern Europe']]);

// get key by non-string
echo $e->key(['Germany', 'Eastern Europe']); // trabant
```

This is how it ought to be used:

```
use vijinho\Enums\Enum;

class Fruits extends Enum
{
    protected static $values = [
        'apple' => 'Apple',
        'pear' => 'Pear',
        'banana' => 'Banana',
        'orange' => 'Orange',
        'grapefruit' => 'Grapefruit',
        'tomato' => 'Cucumber',
    ];
}
```

\### Using ENUM statically

```
// get an enum value by key
echo Fruits::apple(); // Apple
echo Fruits::APPLE(); // Apple

// add a key => value to the enum
Fruits::add([
	'STRAWBERRY' => 'Strawberry',
    'Avocado' => 'Avocado'
]);
// alternative way to fetch a value by key
echo Fruits::value('strawberry'); // Strawberry

// return the key for a value
echo Fruits::key('cucumber'); // tomato

// return all fruits
print_r(Fruits::values());
/*
(
    [apple] => Apple
    [pear] => Pear
    [banana] => Banana
    [orange] => Orange
    [grapefruit] => Grapefruit
    [tomato] => Cucumber
    [STRAWBERRY] => Strawberry
    [Avocado] => Avocado
)
*/
```

\### Using ENUM as an object

Continuing from above...

```
$f = new Fruits;
$f(['mango']); // add a new fruit - magic!
$f(['pineapple' => 'Pineapple']); // add another new fruit
$f->add(['potato' => 'Not a fruit']);
var_dump($f); // special var_dump magic!

object(Fruits)#5 (5) {
  ["overwrite"]=>
  bool(false)
  ["delete"]=>
  bool(false)
  ["capitalize"]=>
  bool(false)
  ["caseSensitive"]=>
  bool(false)
  ["values"]=>
  array(11) {
    ["apple"]=>
    string(5) "Apple"
    ["pear"]=>
    string(4) "Pear"
    ["banana"]=>
    string(6) "Banana"
    ["orange"]=>
    string(6) "Orange"
    ["grapefruit"]=>
    string(10) "Grapefruit"
    ["tomato"]=>
    string(8) "Cucumber"
    ["STRAWBERRY"]=>
    string(10) "Strawberry"
    ["Avocado"]=>
    string(7) "Avocado"
    ["mango"]=>
    string(5) "mango"
    ["pineapple"]=>
    string(9) "Pineapple"
    ["potato"]=>
    string(11) "Not a fruit"
  }
}
```

\### Using ENUM as an array directly

Implements [PHP ArrayAccess interface](http://php.net/manual/en/class.arrayaccess.php)

```
// create a new enum $e
echo "Example 1\n";
$e = new Enum(['apple', 'pear', 'peach']);

// retrieve apple using array access
echo $e['apple']; // apple

// retrieve apple using array access
echo "Example 2\n";
echo isset($e['pear']); // 1

// remove a value
unset($e['pear']);
echo $e;

/*
{
    "apple": "apple",
    "peach": "peach"
}
*/

```

More Usage Examples
-------------------

[](#more-usage-examples)

The class uses static members, so though it can be instantiated with `new` there are some caveats detailed in the [examples](examples) folder:

- Using enums as an [object](examples/object.php)
- Using [static](examples/static.php) enums.
- Strict usage example of [strict](examples/strict.php) enums (case-sensitive, capitalized key)

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

[](#installation)

Add to your `composer.json` the following:

```
"vijinho/enums": "dev-dev-master"
```

Then `composer update` to get it.

then import to the top of your PHP script with:

```
use \vijinho\Enums\Enum;
```

Vijay Mahrra
------------

[](#vijay-mahrra)

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 95.7% 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 ~210 days

Total

3

Last Release

3132d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a5003d887431bec0f3027829a235d126319b5a681efff94873f6d80b5ca9a5de?d=identicon)[vijinho](/maintainers/vijinho)

---

Top Contributors

[![vijinho](https://avatars.githubusercontent.com/u/1376277?v=4)](https://github.com/vijinho "vijinho (67 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (3 commits)")

---

Tags

enumenumsphp7phpenumPHP7enums

### Embed Badge

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

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

###  Alternatives

[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)

PHPackages © 2026

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