Giter VIP home page Giter VIP logo

enum's Introduction

Latest Stable Version Total Downloads Build Status Coverage Status Scrutinizer Code Quality SensioLabs Insight StyleCI License

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

SplEnum

SplEnum 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

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?

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);
    }
}

enum's People

Contributors

peter-gribanov avatar peter279k avatar

Stargazers

 avatar

Watchers

 avatar  avatar

enum's Issues

Enumeration proxy

Make ProxyEnum which analyzes the enumeration and creates a cached version ProxyEnumCache of the enumeration.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.