Enums help you group related values under a single name. They make your code easier to read and less error-prone.
Enums in PHP
enum Name {
case Option1;
case Option2;
case Option3;
}Use the enum keyword followed by the enum name.
Each option is declared with case inside curly braces.
enum Status {
case Pending;
case Approved;
case Rejected;
}enum Direction {
case North;
case South;
case East;
case West;
}enum Size: string {
case Small = 'S';
case Medium = 'M';
case Large = 'L';
}This program defines a TrafficLight enum with three colors. The getAction function returns what to do for each color using a match expression. It then prints the actions for each light.
<?php
enum TrafficLight {
case Red;
case Yellow;
case Green;
}
function getAction(TrafficLight $light): string {
return match($light) {
TrafficLight::Red => 'Stop',
TrafficLight::Yellow => 'Get Ready',
TrafficLight::Green => 'Go',
};
}
echo getAction(TrafficLight::Red) . "\n";
echo getAction(TrafficLight::Yellow) . "\n";
echo getAction(TrafficLight::Green) . "\n";
Enums were introduced in PHP 8.1, so make sure your PHP version supports them.
You can add methods inside enums for extra behavior if needed.
Using enums helps prevent bugs caused by invalid values.
Enums group related constant values under one name.
They improve code clarity and safety by limiting possible values.
Use enums when you have fixed sets of options like states or categories.
Practice
enum in PHP?Solution
Step 1: Understand what enums represent
Enums are used to group related constant values, like fixed options or states.Step 2: Compare with other options
Other options describe unrelated concepts: variables or functions, not enums.Final Answer:
To group related constant values under one name -> Option AQuick Check:
Enums = group constants [OK]
- Thinking enums create dynamic variables
- Confusing enums with classes or functions
- Assuming enums can hold any value
Solution
Step 1: Identify backed enum syntax
Backed enums require a type after the enum name, like: string, and cases with assigned values.Step 2: Check each option
enum Status: string { case Active = 'active'; case Inactive = 'inactive'; } correctly usesenum Status: string { case Active = 'active'; ... }. enum Status { case Active = 'active'; case Inactive = 'inactive'; } misses the type. enum Status: int { case Active = 'active'; case Inactive = 'inactive'; } uses wrong typeintwith string values. enum Status: string { Active = 'active'; Inactive = 'inactive'; } missescasekeyword.Final Answer:
enum Status: string { case Active = 'active'; case Inactive = 'inactive'; } -> Option AQuick Check:
Backed enum syntax = enum Status: string { case Active = 'active'; case Inactive = 'inactive'; } [OK]
- Omitting the enum type after colon
- Forgetting 'case' before each value
- Assigning string values to int enums
enum Direction { case North; case South; case East; case West; }
$dir = Direction::East;
echo $dir->name;Solution
Step 1: Understand enum case name property
In PHP enums,$dir->namereturns the case name as a string.Step 2: Check the assigned case
The variable$diris assignedDirection::East, so$dir->nameis "East".Final Answer:
East -> Option DQuick Check:
Enum case name = East [OK]
- Expecting the value instead of name
- Trying to print enum case directly
- Confusing case name with enum class name
enum Color {
case Red = 1;
case Green = 2;
case Blue;
}Solution
Step 1: Check enum type for backed values
When enum cases have assigned values (like 1, 2), the enum must declare a type (e.g.,: int).Step 2: Analyze the given enum
The enumColorassigns values to some cases but does not specify: intafter the enum name, causing a syntax error.Final Answer:
Missing enum type for backed enum values -> Option BQuick Check:
Backed enums need type declared [OK]
- Not declaring enum type when assigning values
- Assuming cases can mix assigned and unassigned values
- Thinking enum names must be lowercase
enum Status: string {
case Pending = 'pending';
case Approved = 'approved';
case Rejected = 'rejected';
}Which code correctly converts a string
$input to the matching Status enum case or returns null if no match?Solution
Step 1: Understand enum methods for backed enums
PHP backed enums providefrom()which throws error if no match, andtryFrom()which returns null if no match.Step 2: Match requirement
The question asks to return matching enum or null if no match, sotryFrom()is correct.Final Answer:
Status::tryFrom($input) -> Option CQuick Check:
Safe enum conversion = tryFrom() [OK]
- Using from() which throws error on no match
- Trying to instantiate enum with new
- Accessing enum cases as static properties
Ready to prove it?
Pass the test and this pattern moves unknown
