0
0
PHPprogramming~5 mins

Enum backed values in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an enum backed value in PHP?
An enum backed value in PHP is a way to assign a specific scalar value (like int or string) to each enum case, allowing the enum to hold and return that value.
Click to reveal answer
beginner
How do you declare a backed enum with string values in PHP?
Use the syntax: enum Status: string { case Active = 'active'; case Inactive = 'inactive'; } where : string declares the enum as backed by strings.
Click to reveal answer
intermediate
What happens if you try to assign a non-scalar value to a backed enum case?
PHP will throw a fatal error because backed enum cases must have scalar values (int or string) only.
Click to reveal answer
beginner
How can you get the backed value of an enum case in PHP?
You can access the backed value using the ->value property, for example: Status::Active->value returns 'active'.
Click to reveal answer
intermediate
Can backed enums have methods in PHP?
Yes, backed enums can have methods just like regular enums, allowing you to add behavior related to the enum cases.
Click to reveal answer
Which scalar types can be used for backed enum values in PHP?
Aint and string
Bfloat and bool
Carray and object
Dresource and null
How do you declare an enum backed by integers in PHP?
Aenum Status { case Active = 1; case Inactive = 0; }
Benum Status: string { case Active = '1'; case Inactive = '0'; }
Cenum Status: int { case Active = 1; case Inactive = 0; }
Denum Status: float { case Active = 1.0; case Inactive = 0.0; }
What will Status::Active->value return if enum Status: string { case Active = 'active'; }?
A'active'
BStatus::Active
Cnull
DAn error
Can you assign a float value to a backed enum case in PHP?
AOnly if you use a backed enum of type float
BYes, float is allowed
COnly if you cast it to string
DNo, only int and string are allowed
What will happen if you try to assign an array as a backed enum value?
AIt will assign the first element
BFatal error
CIt will be ignored
DIt will convert to string
Explain what a backed enum is and how you use it in PHP.
Think about enums that hold specific values like numbers or words.
You got /4 concepts.
    Describe the rules and limitations for values assigned to backed enum cases in PHP.
    Consider what types PHP allows for enum case values.
    You got /4 concepts.