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

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

palabs/php-enum
===============

Java-like PHP Enum implementation for php 7.4+

0.2.1(6y ago)31.4k↓50%1[1 PRs](https://github.com/PaLabs/php-enum/pulls)MITPHPPHP &gt;=7.4CI passing

Since Apr 3Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/PaLabs/php-enum)[ Packagist](https://packagist.org/packages/palabs/php-enum)[ Docs](http://github.com/PaLabs/php-enum)[ RSS](/packages/palabs-php-enum/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

PHP Enum implementation (Java-like) for php7.4+
===============================================

[](#php-enum-implementation-java-like-for-php74)

[![Build Status](https://camo.githubusercontent.com/e0fc6361bc576d9c2d516b0a935cf037d7af173fee1f0b88572ef20b0603f533/68747470733a2f2f7472617669732d63692e636f6d2f50614c6162732f7068702d656e756d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/PaLabs/php-enum.svg?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/8e94ed877f7d2e7f8b5d224848809a9489f9a5fc3938e4a1e0ce4a9906147890/68747470733a2f2f706f7365722e707567782e6f72672f70616c6162732f7068702d656e756d2f762f737461626c65)](https://packagist.org/packages/palabs/php-enum)[![License](https://camo.githubusercontent.com/ac10878fed785ff96beb5eeafdea11a2081e75cbd28c8f8a26efeedd7ec7bb6a/68747470733a2f2f706f7365722e707567782e6f72672f70616c6162732f7068702d656e756d2f6c6963656e7365)](https://packagist.org/packages/palabs/php-enum)

Benefits
--------

[](#benefits)

- Type-hint: `function someAction(Action $action) {`
- No magick methods or phpdoc comments, only raw enum values
- List of all the possible values
- Own fields or methods in enums

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

[](#installation)

```
composer require palabs/php-enum

```

Create your first enum
----------------------

[](#create-your-first-enum)

```
use PaLabs\Enum\Enum;

class Action extends Enum
{
    public static Action $VIEW, $EDIT;
}
Action::init();
```

That's all!

Examples
--------

[](#examples)

```
function someAction(Action $action) {
   switch($action) {
       case Action::$VIEW:
           // some code
       break;
       case Action::$EDIT:
           // another code
       break;
       default:
           // ...
       break;
   }
}

$viewAction = Action::$VIEW;
if($viewAction->equals(ACTION::$EDIT)) {
// ...
}

$allActions = Action::values();
```

Custom fields in enum
---------------------

[](#custom-fields-in-enum)

```
use PaLabs\Enum\Enum;

class Planet extends Enum
{
   public static Planet
       $MERCURY,
       $VENUS,
       $EARTH,
       $MARS,
       $JUPITER,
       $SATURN,
       $URANUS,
       $NEPTUNE;

   private float $mass;   // in kilograms
   private float $radius; // in meters

   public function __construct(float $mass, float $radius) {
       $this->mass = $mass;
       $this->radius = $radius;
   }

   private const G = 6.67300E-11;

   public function surfaceGravity(): float {
       return self::G * $this->mass / ($this->radius * $this->radius);
   }
   public function surfaceWeight(float $otherMass): float {
       return $otherMass * $this->surfaceGravity();
   }

}

Planet::$MERCURY = new Planet(3.303e+23, 2.4397e6);
Planet::$VENUS = new Planet(4.869e+24, 6.0518e6);
Planet::$EARTH = new Planet(5.976e+24, 6.37814e6);
Planet::$MARS = new Planet(6.421e+23, 3.3972e6);
Planet::$JUPITER = new Planet(1.9e+27,   7.1492e7);
Planet::$SATURN = new Planet(5.688e+26, 6.0268e7);
Planet::$URANUS = new Planet(8.686e+25, 2.5559e7);
Planet::$NEPTUNE = new Planet(1.024e+26, 2.4746e7);
Planet::init();

$yourEarthWeight = 65.0;
$mass = $yourEarthWeight / Planet::$EARTH->surfaceGravity();
foreach (Planet::values() as $planet) {
   sprintf("Your weight on %s is %f%n", $planet->name(), $planet->surfaceWeight($mass));
}
```

Methods
-------

[](#methods)

- `name()` Returns name of the current enum instance (e.g. 'VIEW' for Action::$VIEW)
- `ordinal()` Returns ordinal number of enum instance in all enum instance start with 0. E.g. 0 for Action::$VIEW, 1 for Action::$EDIT
- `equals(Enum $other)` - Tests whether enum instances are equal or not

Static methods
--------------

[](#static-methods)

- `values()` Returns all enum instances
- `valueOf(string $name)` Return enum instance for given name or throwing exception if no enum instance found
- `init()` - Initialize enum (filling enum instances). Need to be called after enum declaration

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance53

Moderate activity, may be stable

Popularity23

Limited adoption so far

Community8

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

Every ~5 days

Total

3

Last Release

2226d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/70ffcbf404633790b0c0161c36d1210e338918e230731ab661572309d5214fff?d=identicon)[lewbor](/maintainers/lewbor)

---

Top Contributors

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

---

Tags

enumphp-enum

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[myclabs/php-enum

PHP Enum implementation

2.7k227.9M637](/packages/myclabs-php-enum)[dasprid/enum

PHP 7.1 enum implementation

382146.0M11](/packages/dasprid-enum)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49644.8M97](/packages/marc-mabe-php-enum)[spatie/laravel-enum

Laravel Enum support

3655.4M31](/packages/spatie-laravel-enum)[cerbero/enum

Zero-dependencies package to supercharge enum functionalities.

359207.5k2](/packages/cerbero-enum)

PHPackages © 2026

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