Recall & Review
beginner
What is an intersection type in PHP?
An intersection type in PHP means a value must satisfy multiple type requirements at the same time. It uses the & symbol to combine types.
Click to reveal answer
beginner
How do you declare a function parameter with an intersection type?
You write the types separated by &. For example: <code>function example(Foo&Bar $param) {}</code> means $param must be both Foo and Bar.Click to reveal answer
intermediate
Can intersection types be used with classes and interfaces?
Yes. Intersection types often combine interfaces or classes to require an object to implement multiple interfaces or extend multiple types.
Click to reveal answer
beginner
What happens if a value does not satisfy all types in an intersection?
PHP will throw a TypeError because the value does not meet all the required type constraints.
Click to reveal answer
intermediate
Write a simple example of a function using intersection types in PHP.
Example:<br><code>interface A { public function a(); }<br>interface B { public function b(); }<br>function test(A& B $obj) { $obj->a(); $obj->b(); }</code><br>This means $obj must implement both A and B.Click to reveal answer
What symbol is used to declare intersection types in PHP?
✗ Incorrect
The & symbol is used to combine types in intersection types.
If a function parameter is declared as
Foo & Bar, what must the argument be?✗ Incorrect
The argument must satisfy both Foo and Bar types.
Can intersection types be used with scalar types like int or string in PHP?
✗ Incorrect
Intersection types in PHP are designed for classes and interfaces, not scalar types.
What error occurs if a value does not meet all intersection type requirements?
✗ Incorrect
PHP throws a TypeError when type requirements are not met.
Which PHP version introduced intersection types?
✗ Incorrect
Intersection types were introduced in PHP 8.1.
Explain what intersection types are and how they are used in PHP.
Think about combining multiple type requirements for one value.
You got /4 concepts.
Describe a scenario where using intersection types in PHP would be helpful.
Imagine a function that needs an object to do two different things.
You got /3 concepts.