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

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

dbstudios/php-enum
==================

A PHP enum implementation that allows complex (i.e. non-scalar) values as enums

2.0.1(11y ago)155PHPPHP &gt;= 5.3.0

Since Jun 12Pushed 10y ago2 watchersCompare

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

READMEChangelog (3)DependenciesVersions (4)Used By (0)

**Please note:** This repository has been deprecated in favor of the Enum package implemented in [PHP-DaybreakCommons](https://github.com/LartTyler/PHP-DaybreakCommons).

PHP Enum - What is it?
----------------------

[](#php-enum---what-is-it)

PHP Enum is an as-close-as-possible "port" of Java-esque enums. PHP does not have native support for enums, and using class constants or static variables is often not enough for a particular project. I wrote PHP Enum for two primary reasons:

1. Class constants can only be a simple value, not something complex like an object instance.
2. Public static properties expose more privilege than necessary to the "outside world".

But Why??? PHP blah blah blah...
--------------------------------

[](#but-why-php-blah-blah-blah)

If you see this code and immediately think "Why would this be necessary?? PHP has XYZ and isn't supposed to work like ABC!!!" then this repo wasn't meant for you, and you should probably move along.

I've seen plenty of people asking "Can I do Java-like enums in PHP", and all the solutions I have seen to date leave much to be desired. Either the recommendation is to use constants (which prevent non-scalar values) or to use public static variables, which (in my opinion) blows the [Principle of Least Privilege](http://en.wikipedia.org/wiki/Principle_of_least_privilege) clean out of the water. PHP Enum seeks to address both of those issues.

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

[](#requirements)

PHP Enum requires PHP 5 &gt;= 5.3.0, as it utilizes the `get_called_class` function which was not introduced until v5.3.0.

Usage
-----

[](#usage)

Using PHP enum is very simple. Below, you can find a simple `Planet` enum created by extending PHP Enum's base class.

```

```

There are a few important things to note in the above example.

First, the Enum class (the base class that all enums should extend) is namespaced to prevent any potential conflicts. The recommended way to access the class is to add `use dbstudios\util\Enum` at the top of your PHP file, below the include for the class file. Alternatively, if you feel like namespacing the class is unnecessary, or if it causes conflicts, you can simply open up `Enum.php` and remove the first line after the opening PHP tag.

Next, any enum should have, at the minimum, one defined method, `init`. This is where we'll register our elements and provide any arguments needed to construct them.

Inside of the `init` method, we use `parent::register` to register new elements. The `register` function uses PHP's `get_called_class` function to determine the class that called the function, so you don't need to worry about specifying which enum the elements should belong to. The first argument to `register` is *always* the name of the element. **Keep in mind:** the element name is case sensitive, which means that if you register an element as `PLUTO`, you'll have to make sure you access it as `PLUTO`. Any subsequent arguments are considered arguments to your enum's, which I'll discuss in a moment. If your constructor does not take any arguments, just pass the name and you're good to go.

In the example above, I've added a constructor to my enum that takes a single argument; the planet's diameter. I can specify this by including additional arguments during my call to `register`. **Caveat:** Unlike Java's enums, which require you to set the class's constructor to private, PHP Enum requires it to be public. The reason for this is that PHP does not offer any way to access private constructors via reflection, which PHP Enum uses to create instances of your enum's elements.

Next, we call `parent::stopRegistration`. This tells PHP Enum that we've now registered all the elements we need to, and that it should prevent any other elements from being added. Since we use `get_called_class` to determine the source of the `register` call, the chance of an element being put into the wrong enum is very low. However, it's always good to call `stopRegistration` so there's an indicator that your enum is fully initialized.

Finally, outside of our class, we add a line to call `Planet::init()`. PHP does not provide any way to evaluate code within a class automatically when it's loaded, so we need to tell PHP what to do in order to begin initialization of our enum.

Now that we have our enum, we can access any of it's values like so:

```

```

The above code will yield output similar to "EARTH has a diameter of 12756.3.".

Additionally, we can iterate over defined elements in an enum.

```

```

Which will yield output identical to the first example for each planet defined in the `Planet` enum.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity60

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

Total

3

Last Release

4170d ago

Major Versions

1.0.0 → 2.0.02014-12-16

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/770791?v=4)[Tyler Lartonoix](/maintainers/LartTyler)[@LartTyler](https://github.com/LartTyler)

---

Top Contributors

[![LartTyler](https://avatars.githubusercontent.com/u/770791?v=4)](https://github.com/LartTyler "LartTyler (16 commits)")

### Embed Badge

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

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

PHPackages © 2026

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