0Pricing
PHP Academy · Lesson

Enumerations (Enums)

Define type-safe sets of values with PHP 8.1 backed and pure enums.

Enumerations (Enums) is a free PHP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Enums?

PHP 8.1 introduced enums — a special type that restricts a variable to a predefined set of named values, making code more expressive and type-safe.

Pure Enum (Unit Enum)

A pure enum has named cases with no associated value. Great for state, direction, or status.

<?php
enum Status {
    case Active;
    case Inactive;
    case Banned;
}

$s = Status::Active;
echo $s->name; // "Active"

Backed Enum

A backed enum assigns a scalar value (int or string) to each case — useful for database storage or API responses.

<?php
enum Color: string {
    case Red   = "red";
    case Green = "green";
    case Blue  = "blue";
}

echo Color::Red->value; // "red"

From and TryFrom

Convert a raw value back to an enum case with from() (throws on invalid) or tryFrom() (returns null on invalid).

<?php
$color = Color::from("red");      // Color::Red
$maybe = Color::tryFrom("purple"); // null

Enum in Type Hints

Use enums as type hints for parameters and return types — PHP enforces only valid cases can be passed.

<?php
function paint(Color $color): void {
    echo "Painting with ".$color->value;
}
paint(Color::Blue); // "Painting with blue"

Enum Cases in match

Enums work beautifully with match expressions for exhaustive handling.

<?php
$label = match($status) {
    Status::Active   => "Active user",
    Status::Inactive => "Inactive account",
    Status::Banned   => "Banned",
};
// PHP can warn if cases are unhandled

Enum Implements Interface

Enums can implement interfaces, allowing polymorphic behavior.

<?php
interface HasLabel {
    public function label(): string;
}

enum Status: string implements HasLabel {
    case Active   = "active";
    case Inactive = "inactive";
    public function label(): string {
        return ucfirst($this->value);
    }
}

Enum Methods

Enums can have methods (but no constructor or mutable properties).

<?php
enum Suit: string {
    case Hearts   = "H";
    case Diamonds = "D";
    case Clubs    = "C";
    case Spades   = "S";

    public function color(): string {
        return match($this) {
            Suit::Hearts, Suit::Diamonds => "red",
            Suit::Clubs,  Suit::Spades  => "black",
        };
    }
}

Enum Constants

Enums can have constants (not cases) for related scalar values.

<?php
enum Direction {
    case North;
    case South;
    const DEFAULT = self::North;
}

Listing Cases

EnumName::cases() returns an array of all enum case objects — useful for generating dropdown options.

<?php
$options = Color::cases();
foreach ($options as $c) {
    echo $c->name.": ".$c->value."\n";
}

Enums vs Class Constants

Enums enforce type safety — you cannot accidentally pass the integer 1 where a Status enum is expected. Class constants provide no such guarantee.

Summary

Enums express fixed sets of values with type safety. Use pure enums for state; backed enums when you need database or API values. Use tryFrom() for safe parsing.

Quick Check

What does Color::tryFrom("invalid") return if no case matches?

Frequently asked questions

Is the “Enumerations (Enums)” lesson free?

Yes — the full text of “Enumerations (Enums)” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Enumerations (Enums)”?

Define type-safe sets of values with PHP 8.1 backed and pure enums. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Enumerations (Enums)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Match Expression and Named Arguments
  2. Nullsafe Operator and Union Types
  3. Enumerations (Enums)
  4. Fibers: Cooperative Concurrency
← Back to PHP Academy