Enum backed values in PHP - Time & Space Complexity
When using enum backed values in PHP, it's important to understand how quickly operations like searching or accessing values happen as the number of enum cases grows.
We want to know how the time to find or use a backed value changes when the enum has more cases.
Analyze the time complexity of the following code snippet.
enum Status: int {
case Pending = 1;
case Active = 2;
case Completed = 3;
case Cancelled = 4;
}
function getStatusName(int $value): ?string {
foreach (Status::cases() as $case) {
if ($case->value === $value) {
return $case->name;
}
}
return null;
}
This code looks through all enum cases to find the name matching a given backed value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all enum cases with
foreach. - How many times: Once for each enum case, until a match is found or all are checked.
As the number of enum cases increases, the time to find a matching value grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows linearly with the number of enum cases.
Time Complexity: O(n)
This means the time to find a backed value grows directly with the number of enum cases.
[X] Wrong: "Looking up a backed value in an enum is instant no matter how many cases there are."
[OK] Correct: Because the code checks each case one by one, the time depends on how many cases exist, so it is not instant for large enums.
Understanding how enum backed value lookups scale helps you explain how your code behaves with more data, showing you think about efficiency in real situations.
"What if we stored the enum cases in a hash map keyed by value? How would the time complexity change?"
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
