Enum 1.0.2 · Library for PHP

Enums as classes, checked by type hints

Each value of an enum is a class that extends divengine\enum. A type hint then accepts the values of its enum and refuses the rest, enums can inherit from each other to form a taxonomy, and a value can carry its own methods. It works on PHP versions without native enums, and next to the native enums of PHP 8.1.

Free software (GPL 3) · PHP 5 or newer · one class, no dependencies

  1. class Cold extends Temperaturea value is a class
  2. dress(new Cold())accepted: a Temperature
  3. dress(new Ice())refused: Ice is a Weapon

Examples

What sets it apart

Each example is a complete script, run with Enum 1.0.2. The output is what it printed.

Checked by type hints

Two enums never mix, even where two constants would have shared a number.

examples/types.php
<?php
// Each value is a class. A type hint accepts the values of its enum and
// refuses the rest, even when two constants would have had the same number.
require __DIR__ . '/../vendor/autoload.php';

use divengine\enum;

abstract class Temperature extends enum {}
class Hot extends Temperature {}
class Cold extends Temperature {}

abstract class Weapon extends enum {}
class Fire extends Weapon {}
class Ice extends Weapon {}

function dress(Temperature $t): string
{
    return $t instanceof Cold ? 'Wear a coat' : 'Wear a T-shirt';
}

echo dress(new Cold()), "\n";
echo dress(new Hot()), "\n";

try {
    echo dress(new Ice()), "\n";
} catch (TypeError $e) {
    echo "Refused: Ice is a Weapon, not a Temperature\n";
}

// The value is the class name, unless the class gives one
echo 'Value of Cold: ', new Cold(), "\n";
Output
Wear a coat
Wear a T-shirt
Refused: Ice is a Weapon, not a Temperature
Value of Cold: Cold

Taxonomies

Enums inherit from each other. A function can ask for one branch, or for the whole tree.

examples/taxonomy.php
<?php
// Enums can inherit from each other: a taxonomy. A function can ask for
// a whole branch, and instanceof tells the branch of a value.
require __DIR__ . '/../vendor/autoload.php';

use divengine\enum;

abstract class Temperature extends enum {}
abstract class Extreme extends Temperature {}
class Fire extends Extreme {}
class Ice extends Extreme {}
abstract class Normal extends Temperature {}
class Hot extends Normal {}
class Cool extends Normal {}
class Cold extends Normal {}

function alarm(Extreme $t): string      // only the extreme ones
{
    return $t instanceof Fire ? 'Call the fire department' : 'Warm up';
}

function advice(Temperature $t): string // any of them
{
    if ($t instanceof Extreme) {
        return alarm($t);
    }
    return $t instanceof Cold ? 'Wear a coat' : 'Enjoy the day';
}

foreach ([new Fire(), new Ice(), new Hot(), new Cool(), new Cold()] as $t) {
    printf("%-5s %-8s %s\n", get_class($t), get_parent_class($t), advice($t));
}

try {
    alarm(new Cool());
} catch (TypeError $e) {
    echo "alarm(Cool) refused: Cool is Normal, not Extreme\n";
}
Output
Fire  Extreme  Call the fire department
Ice   Extreme  Warm up
Hot   Normal   Enjoy the day
Cool  Normal   Enjoy the day
Cold  Normal   Wear a coat
alarm(Cool) refused: Cool is Normal, not Extreme

Values with behavior

A value can have methods and a value of its own, so no switch has to list every case.

examples/behavior.php
<?php
// A value can carry its own behavior and data: methods and a value of its
// own replace a switch over every case.
require __DIR__ . '/../vendor/autoload.php';

use divengine\enum;

abstract class Status extends enum
{
    abstract public function next(): Status;
    public function canEdit(): bool { return false; }
}

class Draft extends Status
{
    public $value = 'draft';
    public function next(): Status { return new Review(); }
    public function canEdit(): bool { return true; }
}

class Review extends Status
{
    public $value = 'in review';
    public function next(): Status { return new Published(); }
}

class Published extends Status
{
    public $value = 'published';
    public function next(): Status { return $this; }
}

$status = new Draft();
for ($i = 0; $i < 4; $i++) {
    printf("%-10s can edit: %s\n", $status, $status->canEdit() ? 'yes' : 'no');
    $status = $status->next();
}
Output
draft      can edit: yes
in review  can edit: no
published  can edit: no
published  can edit: no

Install

Add it to a project

1. Require it

With Composer.

composer require divengine/enum:1.0.2

2. Run an example

Save one of the scripts above in an examples folder of the project, then:

php examples/types.php

The examples use PHP 7 syntax (return types). The examples, with the commands to run them again with Enum 1.0.2, are in the site's repository.