haphpiness

These are things in PHP which make me genuinely_happy();

Enums — Finally, Proper Enumerations

For decades, PHP developers faked enums with class constants, abstract classes full of const values, or — worst of all — magic strings. PHP 8.1 delivered real, first-class enums that are type-safe, autocompletable, and impossible to misuse.

// Pure enum — when you just need named cases
enum Suit {
    case Hearts;
    case Diamonds;
    case Clubs;
    case Spades;
}

// Backed enum — when you need database/API values
enum Status: string {
    case Active = 'active';
    case Inactive = 'inactive';
    case Pending = 'pending';

    public function label(): string {
        return match($this) {
            self::Active => 'Active',
            self::Inactive => 'Inactive',
            self::Pending => 'Awaiting Review',
        };
    }
}

// Type-safe function signatures
function setStatus(Status $status): void {
    // No invalid values possible — the type system enforces it
}

setStatus(Status::Active);     // ✓
setStatus('active');           // TypeError — exactly what we want

Enums can implement interfaces, use traits, and have methods. They're a proper part of the type system, not a bolted-on afterthought. The match expression ensures exhaustive handling — miss a case and your static analyzer catches it.

Significance: Correctness

Enums eliminate an entire category of bugs: invalid state. When a function accepts Status instead of string, it's impossible to pass a misspelled value, an outdated constant, or an empty string. The type system does the validation for you.