Recall & Review
beginner
What is an intersection type in PHP?
An intersection type requires a value to satisfy multiple type constraints at the same time. It means the value must be an instance of all specified types.
Click to reveal answer
beginner
How do you declare an intersection type in PHP 8.1+?
You use the ampersand (&) symbol between types, for example: <code>function example(SomeInterface&AnotherInterface $obj) {}</code> means $obj must implement both interfaces.Click to reveal answer
intermediate
Why use intersection types in practice?
They help ensure that a value meets multiple requirements, improving code safety and clarity by enforcing multiple behaviors or properties at once.
Click to reveal answer
intermediate
Can intersection types be used with classes and interfaces together?
Yes. For example,
ClassA&InterfaceB means the value must be an instance of ClassA and also implement InterfaceB.Click to reveal answer
beginner
What happens if a value does not satisfy all types in an intersection type declaration?
PHP will throw a TypeError at runtime, indicating the value does not meet the required intersection type constraints.
Click to reveal answer
Which symbol is used to declare intersection types in PHP?
✗ Incorrect
The ampersand (&) symbol is used to declare intersection types, meaning the value must satisfy all listed types.
What does the intersection type
A&B mean in PHP?✗ Incorrect
Intersection types require the value to satisfy all types, so it must be both A and B.
Can intersection types include multiple interfaces?
✗ Incorrect
Multiple interfaces can be combined using intersection types to require a value to implement all of them.
What error occurs if a value does not meet an intersection type requirement?
✗ Incorrect
PHP throws a TypeError when a value does not satisfy the intersection type constraints.
Which PHP version introduced intersection types?
✗ Incorrect
Intersection types were introduced in PHP 8.1.
Explain how intersection types improve code safety in PHP.
Think about how requiring multiple interfaces or classes helps avoid wrong values.
You got /4 concepts.
Describe how to declare and use an intersection type with a class and an interface in PHP.
Show a function parameter that requires an object to be both a class instance and implement an interface.
You got /4 concepts.