https://github.com/gpslab/enum
Simple and fast implementation of enumerations
https://github.com/gpslab/enum
enum enumeration php set
Last synced: about 1 year ago
JSON representation
Simple and fast implementation of enumerations
- Host: GitHub
- URL: https://github.com/gpslab/enum
- Owner: gpslab
- License: mit
- Created: 2017-05-16T14:11:42.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-12-19T07:53:28.000Z (over 6 years ago)
- Last Synced: 2025-04-09T16:57:26.174Z (over 1 year ago)
- Topics: enum, enumeration, php, set
- Language: PHP
- Size: 106 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/gpslab/enum)
[](https://packagist.org/packages/gpslab/enum)
[](https://travis-ci.org/gpslab/enum)
[](https://coveralls.io/github/gpslab/enum?branch=master)
[](https://scrutinizer-ci.com/g/gpslab/enum/?branch=master)
[](https://insight.sensiolabs.com/projects/535fd1c2-7b07-47e7-bcaa-702e081f31e8)
[](https://styleci.io/repos/91466048)
[](https://github.com/gpslab/enum)
# PHP enum
Simple and fast implementation of enumerations
## Introduction
### 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](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:
```php
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
```
### Benchmark
Enum benchmark on PHP 7
```
$ tests/benchmark/enum.php 100000
------------------------------- ------------ --------------
Test Memory Avg Duration All
------------------------------- ------------ --------------
Reflection enum 1.52 KiB 1820 ms
Reflection enum (no magic) 1.52 KiB 1718 ms
Explicit enum 0.71 KiB 959 ms
myclabs/php-enum 0.68 KiB 1971 ms
marc-mabe/php-enum 1.59 KiB 2219 ms
marc-mabe/php-enum (no magic) 1.59 KiB 1969 ms
happy-types/enumerable-type 1.81 KiB 2322 ms
------------------------------- ------------ --------------
```
Set benchmark on PHP 7
```
$ tests/benchmark/set.php 100000
-------------------- ------------ --------------
Test Memory Avg Duration All
-------------------- ------------ --------------
Reflection set 1.36 KiB 1238 ms
marc-mabe/php-enum 1.59 KiB 2861 ms
-------------------- ------------ --------------
```
### How to get enum with default value?
```php
final class Color extends ReflectionEnum implements EnumDefault
{
const RED = 1;
const GREEN = 2;
const BLUE = 3;
/**
* @return Color
*/
public static function byDefault()
{
return self::byValue(self::RED);
}
}
```