What if a tiny typo in your code could silently break your whole app? Enums stop that from happening!
Why Enums in PHP? - Purpose & Use Cases
Imagine you are building a website where users can select their favorite fruit from a list. You write code using plain strings like "apple", "banana", or "orange" everywhere to represent these choices.
Later, you need to check if the user picked "apple" or "banana" to give special offers.
Using plain strings everywhere is risky. You might mistype "appel" or "bananna" and your code won't catch it. It becomes hard to find all places where these strings are used. If you want to add a new fruit, you must update many parts of your code manually.
This makes your program slow to fix and easy to break.
Enums in PHP let you define a fixed set of named values in one place. Instead of using plain strings, you use these names. PHP checks for you if you use a wrong name. Adding or changing options is easy and safe.
This keeps your code clean, clear, and less error-prone.
$fruit = "apple"; if ($fruit === "appel") { // Oops, typo! This won't work as expected }
enum Fruit {
case Apple;
case Banana;
case Orange;
}
$fruit = Fruit::Apple;
if ($fruit === Fruit::Apple) {
// Safe and clear
}Enums make your code safer and easier to maintain by grouping related options with clear names checked by PHP itself.
Think of a traffic light system with states like Red, Yellow, and Green. Using enums, you can represent these states clearly and avoid mistakes like "gren" or "yello" in your code.
Manual string checks are error-prone and hard to maintain.
Enums group fixed options with clear names checked by PHP.
This leads to safer, cleaner, and easier-to-update code.
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
