0
0
PhpHow-ToBeginner · 3 min read

How to Use Enum in PHP: Syntax and Examples

In PHP, you use enum to define a set of named constants as a type-safe way to represent fixed values. Declare an enum with enum Name { case VALUE; } and use it by referring to its cases like Name::VALUE.
📐

Syntax

An enum in PHP defines a custom type with a fixed set of possible values called cases. You declare it using the enum keyword followed by the enum name and a block of case statements. Each case represents one possible value.

Enums can be pure (just named cases) or backed (cases have scalar values like strings or integers).

php
enum Status {
    case Pending;
    case Approved;
    case Rejected;
}

// Backed enum example
enum Direction: string {
    case North = 'N';
    case South = 'S';
    case East = 'E';
    case West = 'W';
}
💻

Example

This example shows how to declare a pure enum Status and use it in a function to print messages based on the enum value.

php
<?php

enum Status {
    case Pending;
    case Approved;
    case Rejected;
}

function printStatusMessage(Status $status): void {
    switch ($status) {
        case Status::Pending:
            echo "Status is pending approval.\n";
            break;
        case Status::Approved:
            echo "Status has been approved!\n";
            break;
        case Status::Rejected:
            echo "Status was rejected.\n";
            break;
    }
}

printStatusMessage(Status::Pending);
printStatusMessage(Status::Approved);
printStatusMessage(Status::Rejected);
Output
Status is pending approval. Status has been approved! Status was rejected.
⚠️

Common Pitfalls

  • Trying to assign values to pure enums without declaring them as backed enums causes errors.
  • Using strings or integers directly instead of enum cases loses type safety.
  • Not using the enum type in function parameters can lead to invalid values being passed.
  • Remember enums require PHP 8.1 or higher.
php
<?php
// Wrong: assigning value to pure enum
// enum Color {
//     case Red = 'red'; // Error: pure enums cannot have values
// }

// Right: declare backed enum
enum Color: string {
    case Red = 'red';
    case Blue = 'blue';
}

// Wrong: passing string instead of enum
// function paint(Color $color) {}
// paint('red'); // Error

// Right: pass enum case
function paint(Color $color) {
    echo "Painting with {$color->value}\n";
}
paint(Color::Red);
Output
Painting with red
📊

Quick Reference

ConceptDescriptionExample
Pure enumEnum with named cases only, no valuesenum Status { case Pending; case Approved; }
Backed enumEnum with scalar values (string or int)enum Direction: string { case North = 'N'; }
Access caseUse enum cases with ::Status::Pending
Type hintingUse enum type in function parametersfunction f(Status $s) {}
Get valueFor backed enums, get scalar value$direction->value

Key Takeaways

Use enum in PHP 8.1+ to define fixed sets of named values.
Declare pure enums with cases only or backed enums with scalar values.
Access enum cases with EnumName::CaseName for type safety.
Use enum types in function parameters to avoid invalid values.
Backed enums allow retrieving the scalar value with $enum->value.