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

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

neutronstars/enum
=================

Added an enumeration system as close as possible to PHP 8.1 for earlier versions.

1.4.0(4y ago)2692Apache-2.0PHPPHP ^7.1 | ^8.0

Since Jun 5Pushed 4y ago1 watchersCompare

[ Source](https://github.com/Neutron-Pro/enum-php)[ Packagist](https://packagist.org/packages/neutronstars/enum)[ RSS](/packages/neutronstars-enum/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (13)Used By (2)

Enum library for PHP 7.1 or more.
=================================

[](#enum-library-for-php-71-or-more)

Installation:
-------------

[](#installation)

---

### Only Enum PHP whiteout Doctrine and Symfony:

[](#only-enum-php-whiteout-doctrine-and-symfony)

```
composer require neutronstars/enum

```

---

### For Doctrine

[](#for-doctrine)

```
composer require neutronstars/doctrine-enum-php-type

```

> Docs:

---

### For Symfony

[](#for-symfony)

```
composer require neutronstars/symfony-normalizer-enum-php

```

> Docs:

### For Symfony and Doctrine

[](#for-symfony-and-doctrine)

```
composer require neutronstars/doctrine-enum-php-type
composer require neutronstars/symfony-normalizer-enum-php

```

---

How create an Enum class:
-------------------------

[](#how-create-an-enum-class)

### Method 1

[](#method-1)

```
/**
 * @method static self ONE()
 * @method static self TWO()
 * @method static self THREE()
 */
class MyEnum extends \NeutronStars\Enum\Enum
{
   public const ONE = null;
   public const TWO = null;
   public const THREE = null;
}
```

This enum does not require a constructor. We simply create a list of constants that have no value other than their name.

### Method 2

[](#method-2)

```
/**
 * @method static self ONE()
 * @method static self TWO()
 * @method static self THREE()
 */
class MyStringEnum extends \NeutronStars\Enum\Enum
{
   public const ONE   = 'One';
   public const TWO   = 'Two';
   public const THREE = 'Three';
}
```

```
/**
 * @method static self ONE()
 * @method static self TWO()
 * @method static self THREE()
 */
class MyIntEnum extends \NeutronStars\Enum\Enum
{
   public const ONE   = 1;
   public const TWO   = 2;
   public const THREE = 3;
}
```

---

Use an Enum class:
------------------

[](#use-an-enum-class)

### Get instance of an enum

[](#get-instance-of-an-enum)

```
$two = MyStringEnum::TWO();

echo $two; // return TWO
echo $two->key; // return TWO
echo $two->value; // return Two
```

### Get instance of an enum from a string

[](#get-instance-of-an-enum-from-a-string)

```
$three = MyStringEnum::from('THREE');

echo $three; // THREE
echo $three->key; // return THREE
echo $three->value; // return Three
```

### Compare the instances of an enum

[](#compare-the-instances-of-an-enum)

```
$value = MyStringEnum::tryFrom($_GET['number']);
if ($value === MyStringEnum::ONE()) {
  echo 'The number is ONE !';
}
```

Result:

```
if $_GET['number] is ONE or One or 1 then 'The number is ONE !'
else nothing.

```

---

### Difference between `from` and `tryFrom`.

[](#difference-between-from-and-tryfrom)

If you use "tryFrom" and the parameter value is not found, the method returns null.

If you use "from" and the value of the parameter is not found, an error of type ValueError is raised.

---

### Retrieve all keys of the enum.

[](#retrieve-all-keys-of-the-enum)

```
$cases = MyIntEnum::cases();
```

Result:

```
[
  MyIntEnum::ONE(),
  MyIntEnum::TWO(),
  MyIntEnum::THREE()
]

```

```
foreach (MyIntEnum::cases() as $case) {
    echo '- ' . $case->value;
}
```

Result:

```
- 1
- 2
- 3

```

---

### Serialization and Deserialization

[](#serialization-and-deserialization)

You can serialize an enum with the serialize function of PHP.

```
$serialized = serialize(MyEnum::THREE());
```

But for deserialization, there will be a small difference, You must use the `from` or `tryFrom` method:

```
$deserialized = MyEnum::from(unserialize($deserialized));
// $deserialized = MyEnum::THREE()
```

---

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 85.2% 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 ~3 days

Total

12

Last Release

1774d ago

### Community

Maintainers

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

---

Top Contributors

[![Neutron-Pro](https://avatars.githubusercontent.com/u/72473350?v=4)](https://github.com/Neutron-Pro "Neutron-Pro (23 commits)")[![NeutronStars](https://avatars.githubusercontent.com/u/20863903?v=4)](https://github.com/NeutronStars "NeutronStars (4 commits)")

---

Tags

enumenumerationphpphp-libraryphp7php8reflection

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

PHPackages © 2026

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