PHPackages                             fabiomez/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. fabiomez/php-enum

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

fabiomez/php-enum
=================

A simple Enum PHP project.

1.1.0(3y ago)04MITPHPPHP &gt;=7.4

Since Jun 1Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Fabiomez/php-enum)[ Packagist](https://packagist.org/packages/fabiomez/php-enum)[ RSS](/packages/fabiomez-php-enum/feed)WikiDiscussions main Synced 6d ago

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

Project PHP Enum
================

[](#project-php-enum)

A simple Enum PHP project.

Why?
----

[](#why)

Despite when I wrote this little project PHP8 already exists with its own native Enum. I decided to because I had some free time and some projects still using lower PHP versions. Then I decided to write this with all features that I missed in other PHP Enum libraries.

My version of Enum ensure that all instances of the same *class constant* will always be the same instance. So, does not matter how many times we try to retrieve the enum instance, it will always have the same memory address.

How to install
--------------

[](#how-to-install)

Require via composer

```
composer require Fabiomez/php-enum

```

Or add to your composer.json require session

```
{
...
  "require": {
    ...
    "Fabiomez/php-enum": "1.0.0"
    ...
  },
...
}

```

How to use
----------

[](#how-to-use)

### Declaring

[](#declaring)

First declare a class that extents from `fabiome\Enum\Enum`

```
use Fabiomez\Enum\Enum;

/**
 * @method static self OK
 * @method static self BAD_REQUEST
 */
class HttpCode extends Enum
{
    const OK = 200;
    const BAD_REQUEST = 400;
}
```

- DocBlocks is just for IDE auto complete

### Retrieving

[](#retrieving)

There are three ways to retrieve Enum instance

#### Using from static method

[](#using-from-static-method)

```
$httpCode = HttpCode::from(HttpCode::OK);
```

#### Using tryFrom static method

[](#using-tryfrom-static-method)

```
$httpCode = HttpCode::tryFrom(HttpCode::OK);
```

- The big difference is, while `from` will throw an `InvalidArgumentException` if a constant with the given value does not exist into the declared class, `tryFrom` will just return `null`;

#### Using static magic methods

[](#using-static-magic-methods)

Any declared constant can be called as a static method to retrieve an Enum instance with the constant value.

```
$httpCode = HttpCode::OK();
```

This is the big reason for DocBlocks `@method` declarations.

### Getting the value

[](#getting-the-value)

```
$httpCode = HttpCode::OK();
echo $httpCode->get(); //print int 200
echo (string) $httpCode; //print string 200
```

### Comparing

[](#comparing)

#### By value

[](#by-value)

```
HttpCode::OK()->get() == HttpCode::OK; //true
HttpCode::OK()->get() === HttpCode::OK; //true
HttpCode::OK()->eq(HttpCode::OK); //true
```

#### By memory addressing

[](#by-memory-addressing)

```
HttpCode::OK() === HttpCode::OK(); //true
HttpCode::OK()->is(HttpCode::OK()); //true
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.3% 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 ~10 days

Total

2

Last Release

1436d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3732f5a423c7fafc98cba75328492934d1ee3c227bf802c339f1ae51c8b6e961?d=identicon)[fabiomezini](/maintainers/fabiomezini)

---

Top Contributors

[![fmeziniuolinc](https://avatars.githubusercontent.com/u/86311152?v=4)](https://github.com/fmeziniuolinc "fmeziniuolinc (5 commits)")[![Fabiomez](https://avatars.githubusercontent.com/u/5710555?v=4)](https://github.com/Fabiomez "Fabiomez (1 commits)")

---

Tags

phpenumenumerator

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/fabiomez-php-enum/health.svg)

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

###  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)[kongulov/interact-with-enum

Trait for convenient use of ENUM in PHP

3052.3k2](/packages/kongulov-interact-with-enum)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)

PHPackages © 2026

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