Recall & Review
beginner
What is an enum in PHP?
An enum in PHP is a special type that lets you define a set of named values. It helps group related constants together, making code easier to read and safer.
Click to reveal answer
beginner
How do you define a basic enum in PHP?
Use the
enum keyword followed by the enum name and a list of cases inside curly braces. For example:<br>enum Status { case Pending; case Approved; case Rejected; }Click to reveal answer
intermediate
What is a backed enum in PHP?
A backed enum associates each case with a scalar value like a string or int. This lets you store or compare enum cases easily. Example:<br>
enum Status: string { case Pending = 'pending'; case Approved = 'approved'; }Click to reveal answer
intermediate
How do you get the scalar value of a backed enum case?
You access the
value property of the enum case. For example:<br>$status = Status::Pending; echo $status->value; // outputs 'pending'
Click to reveal answer
advanced
Can enums in PHP have methods?
Yes! Enums can have methods just like classes. This lets you add behavior related to the enum cases. For example:<br><pre>enum Status { case Pending; case Approved; function label() { return match($this) { self::Pending => 'Waiting', self::Approved => 'Done' }; } }</pre>Click to reveal answer
Which keyword is used to define an enum in PHP?
✗ Incorrect
The
enum keyword is used to define enums in PHP.What type of values can backed enums have in PHP?
✗ Incorrect
Backed enums in PHP can have either string or integer scalar values.
How do you access the scalar value of a backed enum case?
✗ Incorrect
The scalar value of a backed enum case is accessed via the
value property.Can PHP enums have methods?
✗ Incorrect
PHP enums can have methods to add behavior related to the enum cases.
Which PHP version introduced enums?
✗ Incorrect
Enums were introduced in PHP 8.1.
Explain what enums are in PHP and why they are useful.
Think about how enums help organize fixed sets of values.
You got /3 concepts.
Describe the difference between pure enums and backed enums in PHP.
Consider how backed enums store extra information.
You got /3 concepts.