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

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

geoffroy-aubry/enum
===================

A simple-to-use PHP class that provides the ability to emulate and create type-safe enumerations.

v1.0.0(12y ago)218LGPL-3.0+PHP &gt;=5.3.3

Since Sep 15Pushed 12y ago1 watchersCompare

[ Source](https://github.com/geoffroy-aubry/Enum)[ Packagist](https://packagist.org/packages/geoffroy-aubry/enum)[ RSS](/packages/geoffroy-aubry-enum/feed)WikiDiscussions stable Synced 2mo ago

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

PHP Enum
========

[](#php-enum)

[![Latest stable version](https://camo.githubusercontent.com/5643fdfbdd15582f92b32394ab153a177bc5c3309a7fdf41ca2e323fcd2ea7c7/68747470733a2f2f706f7365722e707567782e6f72672f67656f6666726f792d61756272792f456e756d2f762f737461626c652e706e67 "Latest stable version")](https://packagist.org/packages/geoffroy-aubry/Enum)[![Build Status](https://camo.githubusercontent.com/a0069383c5b1ad759b89fb86aaaa463f344d2bf40a1c29861f2b0ffc1ac813be/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f67656f6666726f792d61756272792f456e756d2e706e673f6272616e63683d737461626c65)](http://travis-ci.org/geoffroy-aubry/Enum)[![Coverage Status](https://camo.githubusercontent.com/b1606cc83f950f15cb8d1bb1a11f1d565a883c08ba18236facc26392f706aab3/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f67656f6666726f792d61756272792f456e756d2f62616467652e706e673f6272616e63683d737461626c65)](https://coveralls.io/r/geoffroy-aubry/Enum)

This is a simple-to-use PHP class that provides the ability to emulate and create type-safe enumerations.

Table of Contents
-----------------

[](#table-of-contents)

- [Introduction](#introduction)
    - [Definition](#definition)
    - [SplEnum](#splenum)
- [Features](#features)
- [Requirements](#requirements)
- [Usage](#usage)
    - [An enumeration](#an-enumeration)
    - [Magic static calls](#magic-static-calls)
    - [Property calls approach](#property-calls-approach)
    - [Other functionalities](#other-functionalities)
- [Benchmark](#benchmark)
- [Installation](#installation)
- [Documentation](#documentation)
- [Copyrights &amp; licensing](#copyrights--licensing)
- [Change log](#change-log)
- [Continuous integration](#continuous-integration)
- [Git branching model](#git-branching-model)

Introduction
------------

[](#introduction)

### Definition

[](#definition)

> In computer programming, an **enumerated type**(also called **enumeration** or **enum**) is a data type consisting of a set of named values called **elements**, members or enumerators of the type. — [Wikipedia](http://en.wikipedia.org/wiki/Enumerated_type)

### SplEnum

[](#splenum)

[SplEnum](http://php.net/manual/en/class.splenum.php) is not integrated to PHP, you have to install it separately: `$ sudo pecl install SPL_Types`.

In addition, it's not a panacea:

```
class Month extends SplEnum {
    const JANUARY  = 1;
    const FEBRUARY = 2;
}

class Fruit extends SplEnum {
    const APPLE  = 1;
    const ORANGE = 2;
}

// you must create new instance before each use:
$jan   = new Month(Month::JANUARY);
$jan2  = new Month(Month::JANUARY);
$apple = new Fruit(Fruit::APPLE);

var_dump($jan === $jan2);          // false
var_dump($jan === Month::JANUARY); // false
var_dump($jan ==  Fruit::APPLE);   // true
```

Features
--------

[](#features)

*Yet another implementation…*

Several interesting type-safe enumeration's implementations already exists:

- [FlorianWolters/PHP-Component-Core-Enum](https://github.com/FlorianWolters/PHP-Component-Core-Enum)
- [myclabs/php-enum](https://github.com/myclabs/php-enum)
- [eloquent/enumeration](https://github.com/eloquent/enumeration)
- …

But this one provides all the following advantages:

- Type-safe enumeration.
- Whatever the definition of one or more enumerations:
    - each element object is unique under comparison operator ([`==`](http://php.net/manual/en/language.oop5.object-comparison.php))
    - and it is not possible to have 2 distincts instances of the same element, under the identity operator ([`===`](http://php.net/manual/en/language.oop5.object-comparison.php)).
- Supports both static method calls (via [`__callStatic()`](http://www.php.net/manual/en/language.oop5.overloading.php#object.callstatic) magic method) and property calls.
- Property calls approach has a low overhead.
- Supports namespaces to avoid naming collisions.
- Supports autocompletion within an Integrated Development Environment (IDE).
- No need to write any method. Just a simple class with elements as properties.
- Elements cannot be cloned via the magic [`__clone()`](http://php.net/language.oop5.cloning.php#object.clone) method.

Drawbacks:

- Elements are not immutable *(but allow property calls approach with a low overhead)*
- In case of exclusive property calls approach, a call to `MyEnum::buildInstances();` is needed first.

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

[](#requirements)

PHP &gt;= 5.3.3

Usage
-----

[](#usage)

### An enumeration

[](#an-enumeration)

A simple example of enumeration:

```
