This class is an attempt at making the closest approximation to enumerations from other programming languages as is possible in userland PHP code.
The interface and overall design leans heavily on prior enumeration libraries, with SplEnum and MyCLabs\Enum being the two largest influences.
None of the existing libraries that I have run across offer automatic value enumeration, which I felt was a pretty glaring omission.
The only requirement is PHP 5.4 or higher.
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;
}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;
}
}