Enums in PHP - Time & Space Complexity
Enums in PHP help organize related values clearly. Understanding their time complexity shows how fast your program runs when using them.
We want to know how the program's speed changes when working with enums as input grows.
Analyze the time complexity of the following code snippet.
enum Status: string {
case Pending = 'pending';
case Active = 'active';
case Completed = 'completed';
}
function checkStatus(Status $status): string {
return match($status) {
Status::Pending => 'Waiting',
Status::Active => 'In progress',
Status::Completed => 'Done',
};
}
$status = Status::Active;
echo checkStatus($status);
This code defines an enum and a function that returns a message based on the enum value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
matchexpression checks enum cases once per function call. - How many times: It runs once each time
checkStatusis called.
Each call to checkStatus checks a fixed number of enum cases, no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks (one per call) |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with how many times you call the function, but each call does a fixed small amount of work.
Time Complexity: O(n)
This means the total time grows in a straight line with the number of times you use the enum in the function.
[X] Wrong: "Enums slow down the program because they check many cases every time."
[OK] Correct: The number of enum cases is fixed and small, so checking them is very fast and does not grow with input size.
Understanding how enums work and their time cost helps you write clear and efficient code, a skill valued in many programming tasks.
"What if the enum had 1000 cases? How would the time complexity of checking a case change?"
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
