PHP 8 News: Enumerations

Antonio Silva - Mar 9 - - Dev Community

Enumerations were introduced as a new feature in PHP 8.1. Enumerations, often referred to as enums, provide a way to define a set of named constants in PHP. They are a data type that consists of a fixed set of named values, which cannot be changed after declaration.

Defining an Enumeration:

To define an enumeration, you use the enum keyword followed by the name of the enumeration and a list of comma-separated constant values within braces {}.

enum Status {
    case Pending;
    case Approved;
    case Rejected;
}
Enter fullscreen mode Exit fullscreen mode

Using Enumerations:

Once you've defined an enumeration, you can use it just like any other data type in PHP.

$status = Status::Pending;
Enter fullscreen mode Exit fullscreen mode

Accessing Enumerations:

You access enumeration values using the double colon syntax
::.

$status = Status::Pending;
Enter fullscreen mode Exit fullscreen mode

Type Safety:

Enumerations provide type safety. This means that if a function or method expects an enumeration value, passing anything else will result in a type error.

function processStatus(Status $status) {
    // Processing code
}

$status = Status::Approved;
processStatus($status); // Valid

$invalidStatus = 'Pending';
processStatus($invalidStatus); // Type error
Enter fullscreen mode Exit fullscreen mode

Iteration:

You can iterate over the values of an enumeration using a foreach loop.

foreach (Status::cases() as $status) {
    echo $status;
}
Enter fullscreen mode Exit fullscreen mode

Comparison:

Enumerations can be compared for equality using the === operator.

$status1 = Status::Pending;
$status2 = Status::Pending;

if ($status1 === $status2) {
    echo "Statuses are equal";
}
Enter fullscreen mode Exit fullscreen mode

Backing Values:

By default, each constant in an enumeration has an implicit integer value starting from 0. However, you can explicitly assign values to the constants.

enum Status {
    case Pending = 1;
    case Approved = 2;
    case Rejected = 3;
}
Enter fullscreen mode Exit fullscreen mode

Additional Methods:

Enumerations support additional methods like name(), value(), keys(), values(), ordinal(), and jsonSerialize(), which provide various functionalities like getting the name of the constant, its value, keys, values, ordinal position, and serialization respectively.

Enumerations in PHP 8.1 provide a convenient and type-safe way to define a set of related constants, improving code readability, maintainability, and reducing the likelihood of errors related to using incorrect constant values.

More examples

Basic Enumeration:

enum Direction {
    case North;
    case South;
    case East;
    case West;
}

$direction = Direction::North;
echo $direction; // Output: Direction::North
Enter fullscreen mode Exit fullscreen mode

Enumeration with Backing Values:

enum Priority {
    case Low = 1;
    case Medium = 5;
    case High = 10;
}

$priority = Priority::High;
echo $priority->value(); // Output: 10
Enter fullscreen mode Exit fullscreen mode

Type Safety:

function processPriority(Priority $priority) {
    echo "Processing priority: $priority";
}

$priority = Priority::Medium;
processPriority($priority); // Valid

$invalidPriority = 'Low';
processPriority($invalidPriority); // Type error
Enter fullscreen mode Exit fullscreen mode

Iteration:

foreach (Priority::cases() as $priority) {
    echo $priority . "\n";
}
// Output:
// Priority::Low
// Priority::Medium
// Priority::High
Enter fullscreen mode Exit fullscreen mode

Comparison:

$priority1 = Priority::Low;
$priority2 = Priority::Low;

if ($priority1 === $priority2) {
    echo "Priorities are equal\n";
}
// Output: Priorities are equal
Enter fullscreen mode Exit fullscreen mode

Using Methods:

$priority = Priority::Medium;
echo $priority->name() . "\n"; // Output: Medium
echo $priority->ordinal() . "\n"; // Output: 2
echo json_encode($priority) . "\n"; // Output: "2"
Enter fullscreen mode Exit fullscreen mode

These examples showcase different aspects of enumerations in PHP 8.1, including defining enums, assigning backing values, ensuring type safety, iterating over enum values, comparing enum values, and utilizing enumeration methods for various operations.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .