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

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

zrnik/enum
==========

Solution for almost 12 years old Stack Overflow question I returned many times to, is finally packed and ready to require!

v1.0.3(3y ago)01.2k1MITPHPPHP &gt;=7.4CI failing

Since Sep 17Pushed 3y agoCompare

[ Source](https://github.com/Zrnik/Enum)[ Packagist](https://packagist.org/packages/zrnik/enum)[ RSS](/packages/zrnik-enum/feed)WikiDiscussions master Synced today

READMEChangelog (5)Dependencies (3)Versions (6)Used By (1)

Enum for PHP
============

[](#enum-for-php)

[![GitHub](https://camo.githubusercontent.com/eac406aba22ff1e06744283251474db539e52ba48d82414e4cf6258d4fe87d8b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7a726e696b2f656e756d)](https://camo.githubusercontent.com/eac406aba22ff1e06744283251474db539e52ba48d82414e4cf6258d4fe87d8b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7a726e696b2f656e756d)[![Packagist Downloads](https://camo.githubusercontent.com/d1d119016cf86da5634f12902234392481923100a48413b5deb8bfaad1deccb0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f7a726e696b2f656e756d)](https://camo.githubusercontent.com/d1d119016cf86da5634f12902234392481923100a48413b5deb8bfaad1deccb0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f7a726e696b2f656e756d)[![Travis (.com)](https://camo.githubusercontent.com/c19c62d1c52d059a456557cb7fce8401cdd85e2fd3499d4cae2849b84de3ed90/68747470733a2f2f6170692e7472617669732d63692e636f6d2f5a726e696b2f456e756d2e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/c19c62d1c52d059a456557cb7fce8401cdd85e2fd3499d4cae2849b84de3ed90/68747470733a2f2f6170692e7472617669732d63692e636f6d2f5a726e696b2f456e756d2e7376673f6272616e63683d6d6173746572)[![Packagist Version](https://camo.githubusercontent.com/78370e76ba8f8a0b6d5de5b3d13c7663a060853302cea43c1f20944fdc4aa7d6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a726e696b2f656e756d)](https://camo.githubusercontent.com/78370e76ba8f8a0b6d5de5b3d13c7663a060853302cea43c1f20944fdc4aa7d6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a726e696b2f656e756d)

This class was created because of this Stack Overflow question:

Many times I was googling for [Brian Cline's](https://stackoverflow.com/a/254543/3133859)answer. And as many times I copied it, that many times I was extending it for my needs.

**But not again.**

I have rewritten and extended the class, and the result is this package!

Installation
============

[](#installation)

`composer require zrnik/enum`

Change Log
==========

[](#change-log)

- 1.0.0 - Re-Namespace Release (from zrny/enum)

Usage
=====

[](#usage)

#### Create Enum Class

[](#create-enum-class)

```
use Zrnik\Base\Enum;
class DayOfWeek extends Enum
{
    const Sunday = 0;
    const Monday = 1;
    const Tuesday = 2;
    const Wednesday = 3;
    const Thursday = 4;
    const Friday = 5;
    const Saturday = 6;

}
```

#### Compare

[](#compare)

```
if($this->Stardate->DayOfWeek === DayOfWeek::Monday)
{
    $this->spaceTime = new SpaceTime();
     //TODO: Decide if light will be particle or wave
    $this->spaceTime->addComponent(new Light());
}
```

#### Enumerate

[](#enumerate)

```
foreach(DayOfWeek::getValues() as $value)
{
    echo 'It\'s '.DayOfWeek::getName($value).' and im still lazy. '.PHP_EOL;
}
```

```
foreach(DayOfWeek::getNames() as $name)
{
    echo 'It\'s '.$name.' and im'.(DayOfWeek::getValue($name) === DayOfWeek::Monday ? ' ':' still ').'lazy. '.PHP_EOL;
}
```

#### Check

[](#check)

```
if(DayOfWeek::getValue("Monday") === DayOfWeek::Monday) {
    // true
}

if(DayOfWeek::getValue("monday") === DayOfWeek::Monday) {
     // Invalid Argument Exception
}

if(DayOfWeek::getValue("monday", false) === DayOfWeek::Monday) {
    // true, case sensitivity disabled
}

if(DayOfWeek::getValue("monday ", false) === DayOfWeek::Monday) {
    // true, not case sensitive AND it gets trimmed automatically
}
```

##### ???

[](#)

##### PROFIT!

[](#profit)

Methods
=======

[](#methods)

`\Zrnik\Base\Enum::getName(mixed $Value) : array`

Gets constant name by the value.

`\Zrnik\Base\Enum::getValue(string $Name[, bool $caseSensitive = true]) : mixed`

Gets value of a constant by its name. Case sensitivity is modified by second argument.

---

`\Zrnik\Base\Enum::isValidName(string $Name[, bool $caseSensitive = true]) : bool`

Checks if the name is present in the Enum. Case sensitivity is modified by second argument.

`\Zrnik\Base\Enum::isValidValue(string $Value) : bool`

Checks if the value is present in the Enum.

---

`\Zrnik\Base\Enum::toArray([$caseSensitive = true]) : array`

Returns an associative array with all the constants.

`\Zrnik\Base\Enum::getNames([$caseSensitive = true]) : array`

Returns a list with all names defined in an enum.

`\Zrnik\Base\Enum::getValues() : array`

Returns an array with all the values defined in an enum.

---

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

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

Total

5

Last Release

1157d ago

Major Versions

v0.1.0 → v1.0.12023-04-12

PHP version history (2 changes)v1.0.0PHP &gt;=7.4

0.1.x-devPHP &gt;=7.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/55077930?v=4)[Štěpán Zrník](/maintainers/zrnik)[@Zrnik](https://github.com/Zrnik)

---

Top Contributors

[![Zrnik](https://avatars.githubusercontent.com/u/55077930?v=4)](https://github.com/Zrnik "Zrnik (10 commits)")

---

Tags

enum

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

PHPackages © 2026

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