Bird
Raised Fist0
PHPprogramming~10 mins

Enum backed values in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Enum backed values
Define Enum with backed values
↓
Create Enum instance
↓
Access backed value
↓
Use value in code
This flow shows how to define an enum with values, create an instance, and access its backed value.
Execution Sample
PHP
<?php
enum Status: string {
  case Pending = 'pending';
  case Approved = 'approved';
  case Rejected = 'rejected';
}

echo Status::Approved->value;
?>
Defines a string-backed enum Status and prints the value of the Approved case.
Execution Table
StepActionEvaluationResult
1Define enum Status with cases and backed string valuesN/AEnum Status created with cases Pending, Approved, Rejected
2Access Status::ApprovedStatus::ApprovedEnum case Approved selected
3Get value of Status::ApprovedStatus::Approved->value'approved'
4Print valueecho Status::Approved->valueOutput: approved
5End of scriptN/AScript ends
💡 Script ends after printing the backed value of the Approved enum case.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Status::ApprovedN/AEnum case objectEnum case object with value 'approved'Enum case object with value 'approved'
OutputN/AN/AN/A'approved'
Key Moments - 2 Insights
Why do we use ->value after the enum case?
The ->value accesses the backed value of the enum case, as shown in execution_table step 3 where Status::Approved->value returns 'approved'.
Can enum cases have different types of backed values?
Yes, enums can be backed by strings or integers, but all cases must use the same type, as shown in the enum definition in execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output printed at step 4?
A'pending'
B'approved'
C'Approved'
DNo output
💡 Hint
Check the 'Result' column at step 4 in the execution_table.
At which step is the enum case Status::Approved selected?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column to find when Status::Approved is accessed.
If we changed the enum to use integers instead of strings, what would change in the execution_table?
AThe output would be the same string values
BThe enum cases would not be accessible
CThe backed values in step 3 would be integers instead of strings
DThe script would fail to run
💡 Hint
Refer to the 'Evaluation' and 'Result' columns in step 3 about backed values.
Concept Snapshot
PHP Enum backed values:
- Define enum with 'enum Name: type { case A = value; }'
- Backed types: string or int
- Access value with 'EnumCase->value'
- Useful for fixed sets with known values
- Example: enum Status: string { case Pending = 'pending'; }
Full Transcript
This example shows how to define a PHP enum with backed string values. The enum Status has three cases: Pending, Approved, and Rejected, each with a string value. When we access Status::Approved and then use ->value, we get the string 'approved'. The script prints this value. The execution table traces each step: defining the enum, selecting the case, getting the value, printing it, and ending the script. Variables track the enum case object and the output string. Key points include why ->value is needed to get the backed value and that all enum cases share the same backed type. The quiz tests understanding of output, step actions, and what changes if the backed type changes.

Practice

(1/5)
1.

What is the main purpose of using backed enums in PHP?

easy
A. To allow enums to inherit from classes
B. To link enum cases to fixed strings or numbers
C. To enable enums to have methods
D. To make enums mutable

Solution

  1. Step 1: Understand enum backed values

    Backed enums associate each enum case with a fixed string or integer value.
  2. Step 2: Identify the purpose

    This linking helps in storing, comparing, or displaying enum data easily.
  3. Final Answer:

    To link enum cases to fixed strings or numbers -> Option B
  4. Quick Check:

    Backed enums link cases to values = A [OK]
Hint: Backed enums always connect cases to fixed values [OK]
Common Mistakes:
  • Thinking enums can inherit classes
  • Confusing backed enums with mutable enums
  • Assuming enums cannot have methods
2.

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';
}
easy
A. enum Status: string { case ACTIVE = 'active'; case INACTIVE = 'inactive'; }
B. enum Status { case ACTIVE = 'active'; case INACTIVE = 'inactive'; }
C. enum Status: int { case ACTIVE = 'active'; case INACTIVE = 'inactive'; }
D. enum Status: string { ACTIVE = 'active'; INACTIVE = 'inactive'; }

Solution

  1. Step 1: Check enum declaration syntax

    Backed enums require specifying the type after the enum name, e.g., : string.
  2. Step 2: Verify case declarations

    Each case must use the case keyword and assign a value with =.
  3. Final Answer:

    enum Status: string { case ACTIVE = 'active'; case INACTIVE = 'inactive'; } -> Option A
  4. Quick Check:

    Backed enum syntax with type and cases = B [OK]
Hint: Backed enums need type after enum name and case keyword [OK]
Common Mistakes:
  • Omitting the type after enum name
  • Not using 'case' keyword before cases
  • Using wrong type like int for string values
3.

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;
medium
A. Color::GREEN
B. 2
C. GREEN
D. Error: No case for value 2

Solution

  1. Step 1: Understand Color::from(2)

    The from() method returns the enum case matching the value 2, which is GREEN.
  2. Step 2: Access the name property

    Using ->name returns the case name as a string, here "GREEN".
  3. Final Answer:

    GREEN -> Option C
  4. Quick Check:

    from(2) returns GREEN name = GREEN [OK]
Hint: from(value) returns enum case; ->name gives case name [OK]
Common Mistakes:
  • Printing the value instead of the name
  • Confusing from() with tryFrom()
  • Expecting full enum class name output
4.

Identify the error in the following PHP enum backed values code:

enum Status: string {
    case PENDING = 'pending';
    case COMPLETED = 'completed';
}

$status = Status::from('done');
medium
A. No error, code runs fine
B. Compile error: Enum must use int type
C. Syntax error: Missing semicolon after enum cases
D. Runtime error: Value 'done' not found in enum cases

Solution

  1. Step 1: Check the from() method usage

    The from() method throws an error if the value does not match any enum case.
  2. Step 2: Analyze the value 'done'

    Since 'done' is not a defined case value, a runtime error occurs.
  3. Final Answer:

    Runtime error: Value 'done' not found in enum cases -> Option D
  4. Quick Check:

    from() with invalid value causes runtime error = D [OK]
Hint: from() fails if value not in enum cases [OK]
Common Mistakes:
  • Assuming from() returns null on failure
  • Thinking syntax error due to semicolons
  • Believing enum must be int type only
5.

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:

hard
A. $dir = Direction::tryFrom('east');
B. $dir = Direction::from('southwest');
C. $dir = Direction::from('up');
D. $dir = Direction::tryFrom('down');

Solution

  1. Step 1: Understand from() vs tryFrom()

    from() throws error if value not found; tryFrom() returns null instead.
  2. Step 2: Check each option's value

    Only tryFrom('east') matches a valid case and safely returns the enum case.
  3. Final Answer:

    $dir = Direction::tryFrom('east'); -> Option A
  4. Quick Check:

    tryFrom() safely converts valid value = C [OK]
Hint: Use tryFrom() to avoid errors on invalid values [OK]
Common Mistakes:
  • Using from() with invalid values causing errors
  • Expecting tryFrom() to throw errors
  • Confusing enum case names with values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard