0
0
PHPprogramming~10 mins

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

Choose your learning style9 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.