Skip to content

drrcknlsn/enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Enum

What is this?

This class is an attempt at making the closest approximation to enumerations from other programming languages as is possible in userland PHP code.

Credits

The interface and overall design leans heavily on prior enumeration libraries, with SplEnum and MyCLabs\Enum being the two largest influences.

Raison d'être

None of the existing libraries that I have run across offer automatic value enumeration, which I felt was a pretty glaring omission.

Requirements

The only requirement is PHP 5.4 or higher.

Installation

Declaration

Declaring an enumeration is done by extending the base class and providing your list of enumerators as private static properties:

use Drrcknlsn\Enum\Enum;

require 'vendor/autoload.php';

class DayOfWeek extends Enum
{
    private static
        $MONDAY = 1, // ISO-8601
        $TUESDAY,
        $WEDNESDAY,
        $THURSDAY,
        $FRIDAY,
        $SATURDAY,
        $SUNDAY;
}

Usage

The enumerators are accessed by magic static methods:

$day = DayOfWeek::WEDNESDAY(); // hump daaaay!

They can be typehinted like any other class:

function isWeekend(DayOfWeek $day) {
    return $day === DayOfWeek::SATURDAY()
        || $day === DayOfWeek::SUNDAY();
}

You can also iterate an enumeration's values:

foreach (DayOfWeek::getValues() as $day) {
    if ($day !== DayOfWeek::TUESDAY()) {
        continue;
    }
}

About

An enumeration implementation for PHP.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages