2017 © Pedro Peláez
 

library enum

PHP Enums without using Reflection

image

jesseschalken/enum

PHP Enums without using Reflection

  • Thursday, February 4, 2016
  • by jesseschalken
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

php-enum

Fast and safe enums for PHP 5.3+ without using reflection., (*1)

Objectives: - Be fast. Checking an enum value should be O(1). - Be concise. The author of the enum should have to write as minimal code as possible without compromising the other objectives. - Be safe. An instance of the concrete Enum class may only contain a valid value. - Without Reflection. In particular: - Don't use ReflectionClass to get a list of a class's constants, since this violates the developer's expectation that a class constant not referenced via Class::CONSTANT is unused and can be removed. - Don't use __call or __callStatic to create pseudo-methods, since this violates the developer's expectation that a call to a method of the form Class::method() or $object->method() which cannot be found in the code is an error., (*2)

Example:, (*3)

use JesseSchalken\Enum\IntEnum;

class Day extends IntEnum {
    const MONDAY    = 0;
    const TUESDAY   = 1;
    const WEDNESDAY = 2;
    const THURSDAY  = 3;
    const FRIDAY    = 4;
    const SATURDAY  = 5;
    const SUNDAY    = 6;

    public static function values() {
        return array(
            self::MONDAY,
            self::TUESDAY,
            self::WEDNESDAY,
            self::THURSDAY,
            self::FRIDAY,
            self::SATURDAY,
            self::SUNDAY,
        );
    }
}

function needs_day(Day $d) {
    if ($d->value() == Day::FRIDAY) {
        print "It's Friday, Friday...";
    }
}

Day::check(Day::TUESDAY); // okay
Day::check(2); // okay
Day::check(7); // throws EnumException

Day::valid(Day::SUNDAY); // true
Day::valid('foo'); // false

$day = new Day(Day::MONDAY);

needs_day($day);
use JesseSchalken\Enum\StringEnum;

class Color extends StringEnum {
    const RED    = 'red';
    const ORANGE = 'orange';
    const YELLOW = 'yellow';
    const GREEN  = 'green';
    const BLUE   = 'blue';
    const PURPLE = 'purple';

    public static function values() {
        return array(
            self::RED,
            self::ORANGE,
            self::YELLOW,
            self::GREEN,
            self::BLUE,
            self::PURPLE,
        );
    }
}

function needs_color(Color $c) {
    switch ($c->value()) {
        case Color::RED:
            print 'is red';
            break;
        //...
    }
}

$color = new Color(Color::RED);

needs_color($color);

The Versions

04/02 2016

dev-master

9999999-dev

PHP Enums without using Reflection

  Sources   Download

MIT

The Development Requires

by Jesse Schalken

15/01 2016

v1.0

1.0.0.0

PHP Enums without using Reflection

  Sources   Download

MIT

The Development Requires

by Jesse Schalken