Enums with backed values let you give each option a fixed value like a number or string. This helps when you want to connect names to real data.
Enum backed values in PHP
enum Name: type {
case OPTION1 = value1;
case OPTION2 = value2;
// ...
}The type can be int or string.
Each case must have a unique value of the specified type.
enum Status: string {
case Pending = 'pending';
case Approved = 'approved';
case Rejected = 'rejected';
}enum Direction: int {
case Up = 1;
case Down = 2;
case Left = 3;
case Right = 4;
}This program defines a backed enum for days with numbers. It prints the value for Monday and Friday, then converts the number 3 back to the enum and prints its name.
<?php
enum DayOfWeek: int {
case Monday = 1;
case Tuesday = 2;
case Wednesday = 3;
case Thursday = 4;
case Friday = 5;
case Saturday = 6;
case Sunday = 7;
}
function printDayValue(DayOfWeek $day): void {
echo "The value of {$day->name} is {$day->value}.\n";
}
printDayValue(DayOfWeek::Monday);
printDayValue(DayOfWeek::Friday);
// Convert value back to enum
$day = DayOfWeek::from(3);
echo "Day with value 3 is {$day->name}.\n";
You can use ::from(value) to get the enum case from a value. It throws an error if the value is invalid.
You can also use ::tryFrom(value) which returns null if the value is invalid.
Enum backed values link enum cases to fixed strings or numbers.
This helps when you want to store, compare, or display enum data easily.
Use from() or tryFrom() to convert values back to enum cases.
Practice
What is the main purpose of using backed enums in PHP?
Solution
Step 1: Understand enum backed values
Backed enums associate each enum case with a fixed string or integer value.Step 2: Identify the purpose
This linking helps in storing, comparing, or displaying enum data easily.Final Answer:
To link enum cases to fixed strings or numbers -> Option BQuick Check:
Backed enums link cases to values = A [OK]
- Thinking enums can inherit classes
- Confusing backed enums with mutable enums
- Assuming enums cannot have methods
Which of the following is the correct syntax to declare a backed enum with string values in PHP?
enum Status: string {
case ACTIVE = 'active';
case INACTIVE = 'inactive';
}Solution
Step 1: Check enum declaration syntax
Backed enums require specifying the type after the enum name, e.g.,: string.Step 2: Verify case declarations
Each case must use thecasekeyword and assign a value with=.Final Answer:
enum Status: string { case ACTIVE = 'active'; case INACTIVE = 'inactive'; } -> Option AQuick Check:
Backed enum syntax with type and cases = B [OK]
- Omitting the type after enum name
- Not using 'case' keyword before cases
- Using wrong type like int for string values
What will be the output of the following PHP code?
enum Color: int {
case RED = 1;
case GREEN = 2;
case BLUE = 3;
}
echo Color::from(2)->name;Solution
Step 1: Understand Color::from(2)
Thefrom()method returns the enum case matching the value 2, which isGREEN.Step 2: Access the name property
Using->namereturns the case name as a string, here "GREEN".Final Answer:
GREEN -> Option CQuick Check:
from(2) returns GREEN name = GREEN [OK]
- Printing the value instead of the name
- Confusing from() with tryFrom()
- Expecting full enum class name output
Identify the error in the following PHP enum backed values code:
enum Status: string {
case PENDING = 'pending';
case COMPLETED = 'completed';
}
$status = Status::from('done');Solution
Step 1: Check the from() method usage
Thefrom()method throws an error if the value does not match any enum case.Step 2: Analyze the value 'done'
Since 'done' is not a defined case value, a runtime error occurs.Final Answer:
Runtime error: Value 'done' not found in enum cases -> Option DQuick Check:
from() with invalid value causes runtime error = D [OK]
- Assuming from() returns null on failure
- Thinking syntax error due to semicolons
- Believing enum must be int type only
Given the following enum, which code snippet correctly converts a string value to an enum case safely without throwing an error?
enum Direction: string {
case NORTH = 'north';
case SOUTH = 'south';
case EAST = 'east';
case WEST = 'west';
}Choose the correct usage:
Solution
Step 1: Understand from() vs tryFrom()
from()throws error if value not found;tryFrom()returns null instead.Step 2: Check each option's value
OnlytryFrom('east')matches a valid case and safely returns the enum case.Final Answer:
$dir = Direction::tryFrom('east'); -> Option AQuick Check:
tryFrom() safely converts valid value = C [OK]
- Using from() with invalid values causing errors
- Expecting tryFrom() to throw errors
- Confusing enum case names with values
Ready to prove it?
Pass the test and this pattern moves unknown
