Complete the code to declare a backed enum with string values.
<?php
enum Status: string {
case Pending = [1];
case Approved = 'approved';
case Rejected = 'rejected';
}
The backed enum values must be strings enclosed in quotes. So 'pending' is correct.
Complete the code to get the backed value of the enum case.
<?php
enum Direction: string {
case North = 'N';
case South = 'S';
}
echo Direction::North->[1];
In PHP enums, the backed value of a case is accessed using the value property.
Fix the error in the code to get the enum case from a backed value.
<?php
enum Status: int {
case Draft = 0;
case Published = 1;
}
$status = Status::from([1]);
echo $status->name;
The from method expects the backed value with the correct type. Since the enum is backed by int, use the integer 1, not a string.
Fill both blanks to create a backed enum and get a case from a backed value.
<?php enum Level: [1] { case Low = 1; case High = 2; } echo Level::[2]->value;
The enum is backed by integers, so use int. To get the value of the Low case, use Level::Low.
Fill all three blanks to define a backed enum, get a case from a value, and print its name.
<?php enum Color: [1] { case Red = 'R'; case Blue = 'B'; case Green = 'G'; } $color = Color::from([2]); echo $color->[3];
The enum is backed by strings, so use string. To get the case from the backed value 'B', use from('B'). To print the case name, use the name property.