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

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

vjik/php-enum
=============

PHP Enum Implementation

4.0.0(4y ago)209.9k1BSD-3-ClausePHPPHP ^8.0

Since Jul 14Pushed 4y ago1 watchersCompare

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

READMEChangelog (9)Dependencies (3)Versions (10)Used By (0)

PHP Enum Implementation
=======================

[](#php-enum-implementation)

[![Latest Stable Version](https://camo.githubusercontent.com/eb5696693ac753c07e86ac63a960d62f7e902fd60dbfa976e67ba89537c2d054/68747470733a2f2f706f7365722e707567782e6f72672f766a696b2f7068702d656e756d2f762f737461626c652e706e67)](https://packagist.org/packages/vjik/php-enum)[![Total Downloads](https://camo.githubusercontent.com/055700ae71e217cffad7bf938825433a7485b41c2ac3b85916df66a839652c80/68747470733a2f2f706f7365722e707567782e6f72672f766a696b2f7068702d656e756d2f646f776e6c6f6164732e706e67)](https://packagist.org/packages/vjik/php-enum)[![Build status](https://github.com/vjik/php-enum/workflows/build/badge.svg)](https://github.com/vjik/php-enum/actions?query=workflow%3Abuild)[![Mutation testing badge](https://camo.githubusercontent.com/346730582ddc49dac3bb7d51371150656cb16e933b172a02c06b5d706cbfa423/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246766a696b2532467068702d656e756d2532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/vjik/php-enum/master)[![static analysis](https://github.com/vjik/php-enum/workflows/static%20analysis/badge.svg)](https://github.com/vjik/php-enum/actions?query=workflow%3A%22static+analysis%22)

The package implement ideas from [RFC Enumerations](https://wiki.php.net/rfc/enumerations) and provide abstract class `Enum` that intended to create [enumerated objects](https://en.wikipedia.org/wiki/Enumerated_type) with support [extra data](#extradata) and auxiliary static functions [`values()`](#values), [`cases()`](#cases) and [`isValid()`](#isValid).

Requirements
------------

[](#requirements)

- PHP 8.0 or higher.

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

[](#installation)

The package could be installed with composer:

```
composer require vjik/php-enum --prefer-dist
```

General usage
-------------

[](#general-usage)

### Declaration of class

[](#declaration-of-class)

```
use Vjik\Enum\Enum;

/**
 * @method static self NEW()
 * @method static self PROCESS()
 * @method static self DONE()
 */
final class Status extends Enum
{
    private const NEW = 'new';
    private const PROCESS = 'process';
    private const DONE = 'done';
}
```

### Creating an object

[](#creating-an-object)

#### By static method `from()`

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

```
$process = Status::from('process');
```

On create object with invalid value throws `ValueError`.

#### By static method `tryFrom()`

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

```
$process = Status::tryFrom('process'); // Status object with value "process"
$process = Status::tryFrom('not-exists'); // null
```

On create object with invalid value returns `null`.

#### By static method with a name identical to the constant name

[](#by-static-method-with-a-name-identical-to-the-constant-name)

Static methods are automatically implemented to provide quick access to an enum value.

```
$process = Status::PROCESS();
```

### Getting value and name

[](#getting-value-and-name)

```
Status::DONE()->getName(); // DONE
Status::DONE()->getValue(); // done
```

### Class with extra data

[](#class-with-extra-data)

Set data in the protected static function `data()` and create getters using the protected method `getPropertyValue()`. Also you can create getter using protected method `match()`.

```
use Vjik\Enum\Enum;

/**
 * @method static self CREATE()
 * @method static self UPDATE()
 */
final class Action extends Enum
{
    private const CREATE = 1;
    private const UPDATE = 2;

    protected static function data(): array
    {
        return [
            self::CREATE => [
                'tip' => 'Create document',
            ],
            self::UPDATE => [
                'tip' => 'Update document',
            ],
        ];
    }

    public function getTip(): string
    {
        /** @var string */
        return $this->getPropertyValue('tip');
    }

    public function getColor(): string
    {
        return $this->match([
            self::CREATE => 'red',
            self::UPDATE => 'blue',
        ]);
    }

    public function getCode(): int
    {
        return $this->match([
            self::CREATE => 1,
        ], 99);
    }
}
```

Usage:

```
echo Action::CREATE()->getTip();
echo Action::CREATE()->getColor();
echo Action::CREATE()->getCode();
```

### Auxiliary static functions

[](#auxiliary-static-functions)

####  List of values `values()`

[](#-list-of-values-values)

Returns list of values.

```
// [1, 2]
Action::values();
```

####  List of objects `cases()`

[](#-list-of-objects-cases)

Returns list of objects:

```
// [$createObject, $updateObject]
Action::cases();
```

####  Validate value `isValid()`

[](#-validate-value-isvalid)

Check if value is valid on the enum set.

```
Action::isValid(1); // true
Action::isValid(99); // false
```

### Casting to string

[](#casting-to-string)

`Enum` support casting to string (using magic method `__toString`). The value is returned as a string.

```
echo Status::DONE(); // done
```

Testing
-------

[](#testing)

### Unit testing

[](#unit-testing)

The package is tested with [PHPUnit](https://phpunit.de/). To run tests:

```
./vendor/bin/phpunit
```

### Mutation testing

[](#mutation-testing)

The package tests are checked with [Infection](https://infection.github.io/) mutation framework. To run it:

```
./vendor/bin/infection
```

### Static analysis

[](#static-analysis)

The code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis:

```
./vendor/bin/psalm
```

License
-------

[](#license)

The PHP Enum implementation is free software. It is released under the terms of the BSD License. Please see [`LICENSE`](./LICENSE.md) for more information.

Credits
-------

[](#credits)

Version 3 of this package is inspired by [`myclabs/php-enum`](https://github.com/myclabs/php-enum).

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity74

Established project with proven stability

 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 ~176 days

Recently: every ~340 days

Total

9

Last Release

1818d ago

Major Versions

1.2.0 → 2.0.02017-09-04

2.2.0 → 3.0.02021-05-26

3.0.0 → 4.0.02021-05-27

PHP version history (2 changes)1.0.0PHP &gt;=5.4.0

3.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/53e5ee1dedd50f71e4aeeac2929f786cdfb400359d4776e6cd806388d0d5df2c?d=identicon)[vjik](/maintainers/vjik)

---

Top Contributors

[![vjik](https://avatars.githubusercontent.com/u/525501?v=4)](https://github.com/vjik "vjik (19 commits)")

---

Tags

enumphpphp-enumphpenum

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

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