PHP: Enums

Carlos Costa

A brief introduction.

enums are a set of symbolic names (members) bound to unique, constant values. Within an enumeration, the members can be compared by identity, and the enumeration itself can be iterated over.

Let’s to examples:

enum Colors
{
    case RED;
    case GREEN;
    case BLUE;
}

Here’s a simple enum with three members: RED, GREEN, and BLUE. Each of these members is bound to a unique, constant value: 0, 1, and 2, respectively. The members can be compared by identity, and the enum itself can be iterated over.

Now, let’s see how to use it:

function getColorName(Colors $color): string
{
    return match ($color) {
        Colors::RED => 'red',
        Colors::GREEN => 'green',
        Colors::BLUE => 'blue',
    };
}

Here we have a function that takes a Colors enum as an argument and returns a string.

Values


Enums can have values other than integers. For example:

enum Colors: string
{
    case RED = "red";
    case GREEN = "green";
    case BLUE = "blue";
}

Then, we can use recover the values like this:

echo Colors::RED->value; // red

All values can be recovered using the cases() method:

foreach (Colors::cases() as $color) {
    echo $color->value;
} //red, green, blue

Using from() or tryFrom() methods, we can recover the enum from the value:

Colors::from("red"); // Colors::RED
Colors::tryFrom("red"); // Colors::RED

The difference between from() and tryFrom() is that from() throws an exception if the value is not found, while tryFrom() returns null.

Methods


Enums can have methods:

enum Colors: string
{
    case RED = "red";
    case GREEN = "green";
    case BLUE = "blue";

    public function toHex(): string
    {
        return match ($this) {
            Colors::RED => "#ff0000",
            Colors::GREEN => "#00ff00",
            Colors::BLUE => "#0000ff",
        };
    }
}

var_dump(Colors::RED->toHex()); // string(7) "#ff0000"

In the previous example, we have a method called toHex() that returns the hexadecimal value of the color. That method can be called on any instance of the enum.

Interfaces


Enums can implement interfaces:

interface ColorTypes
{
    public function toHex(): string;
    public function toRgb(): string;
}

enum Colors: string implements ColorTypes
{
    case RED = "red";
    case GREEN = "green";
    case BLUE = "blue";

    public function toHex(): string
    {
        return match ($this) {
            Colors::RED => "#ff0000",
            Colors::GREEN => "#00ff00",
            Colors::BLUE => "#0000ff",
        };
    }

    public function toRgb(): string
    {
        return match ($this) {
            Colors::RED => "rgb(255, 0, 0)",
            Colors::GREEN => "rgb(0, 255, 0)",
            Colors::BLUE => "rgb(0, 0, 255)",
        };
    }
}

var_dump(Colors::RED->toHex()); // string(7) "#ff0000"
var_dump(Colors::RED->toRgb()); // string(15) "rgb(255, 0, 0)"

Traits


Enums can use traits:

trait HexColors
{
    public function getHexColor(): string
    {
        return match ($this) {
            Colors::RED => "#FF0000",
            Colors::GREEN => "#00FF00",
            Colors::BLUE => "#0000FF",
            default => "#000000",
        };
    }

    public function getRgbColor(): string
    {
        return match ($this) {
            Colors::RED => "rgb(255, 0, 0)",
            Colors::GREEN => "rgb(0, 255, 0)",
            Colors::BLUE => "rgb(0, 0, 255)",
            default => "rgb(0, 0, 0)",
        };
    }
}

enum Colors: string
{
    use HexColors;

    case RED = "red";
    case GREEN = "green";
    case BLUE = "blue";
}

$red = Colors::RED;
echo $red->getHexColor(); // #FF0000
echo $red->getRgbColor(); // rgb(255, 0, 0)

Resources