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?
A,
B|
C/
D&
✗ 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?
AAn object that is both Foo and Bar
BAny type of object
CAn object that is neither Foo nor Bar
DAn object that is either Foo or Bar
✗ Incorrect
The argument must satisfy both Foo and Bar types.
Can intersection types be used with scalar types like int or string in PHP?
AYes, always
BNo, intersection types only work with classes and interfaces
COnly with int
DOnly with string
✗ 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?
ATypeError
BParseError
CWarning
DNotice
✗ Incorrect
PHP throws a TypeError when type requirements are not met.
Which PHP version introduced intersection types?
APHP 7.4
BPHP 8.2
CPHP 8.1
DPHP 8.0
✗ 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.
Practice
(1/5)
1. What does an intersection type in PHP represent? function example(SomeType&OtherType $param) {}
easy
A. A type that requires the value to satisfy both SomeType and OtherType.
B. A type that accepts either SomeType or OtherType.
C. A type that only accepts SomeType but ignores OtherType.
D. A type that combines SomeType and OtherType into a new class.
Solution
Step 1: Understand intersection type syntax
In PHP, the & symbol between types means the value must satisfy all listed types simultaneously.
Step 2: Interpret the function parameter
The parameter type SomeType&OtherType means the argument must be both SomeType and OtherType at the same time.
Final Answer:
A type that requires the value to satisfy both SomeType and OtherType. -> Option A
Quick Check:
Intersection type = must satisfy all types [OK]
Hint: Remember & means AND, so all types must match [OK]
Common Mistakes:
Confusing & with | (union type)
Thinking it creates a new combined class
Assuming it accepts either type instead of both
2. Which of the following is the correct syntax for declaring an intersection type in a function parameter in PHP?
easy
A. function test(A & B $param) {}
B. function test(A | B $param) {}
C. function test(A, B $param) {}
D. function test(A && B $param) {}
Solution
Step 1: Recall intersection type syntax
PHP uses a single ampersand & to declare intersection types between multiple types.
Step 2: Check each option
function test(A & B $param) {} uses & correctly. function test(A | B $param) {} uses | which is union type. function test(A, B $param) {} uses comma which is invalid. function test(A && B $param) {} uses && which is not valid syntax.
Final Answer:
function test(A & B $param) {} -> Option A
Quick Check:
Intersection type uses & symbol [OK]
Hint: Use single & between types for intersection [OK]
Common Mistakes:
Using | instead of &
Using commas to separate types
Using && instead of &
3. What will be the output of the following PHP code?
interface A { public function foo(): string; } interface B { public function bar(): string; }
class C implements A, B { public function foo(): string { return 'foo'; } public function bar(): string { return 'bar'; } }
function test(A & B $obj) { echo $obj->foo() . ' ' . $obj->bar(); }
$c = new C(); test($c);
medium
A. TypeError at runtime
B. foo
C. bar
D. foo bar
Solution
Step 1: Understand class and interfaces
Class C implements both interfaces A and B, so it has methods foo() and bar().
Step 2: Analyze function test parameter and call
The function test requires an argument that satisfies both A and B (intersection type). Object $c satisfies both. The function echoes foo() and bar() results concatenated with a space.
Final Answer:
foo bar -> Option D
Quick Check:
Object satisfies both interfaces, prints both methods [OK]
Hint: Intersection means object must have all methods used [OK]
Common Mistakes:
Expecting only one method output
Thinking it causes a TypeError
Confusing intersection with union types
4. Identify the error in this PHP code using intersection types:
interface X { public function x(): void; } interface Y { public function y(): void; }
class Z implements X { public function x(): void {} }
function foo(X & Y $param) { $param->x(); $param->y(); }
$z = new Z(); foo($z);
medium
A. Function foo has wrong parameter syntax for intersection types.
B. Method y() is missing in function foo.
C. Class Z does not implement interface Y, so it cannot satisfy X & Y.
D. No error, code runs fine.
Solution
Step 1: Check class Z implementation
Class Z implements only interface X, not Y, so it does not satisfy the intersection type X & Y.
Step 2: Analyze function call with $z
Function foo requires a parameter that implements both X and Y. Passing $z causes a TypeError because $z lacks Y.
Final Answer:
Class Z does not implement interface Y, so it cannot satisfy X & Y. -> Option C
Quick Check:
Intersection requires all interfaces implemented [OK]
Hint: Check all interfaces are implemented for intersection [OK]
Common Mistakes:
Assuming partial interface implementation is enough
Thinking syntax is wrong instead of logic
Ignoring missing methods from interfaces
5. Given these interfaces and classes:
interface Logger { public function log(string $msg): void; } interface FileWriter { public function write(string $data): void; }
class FileLogger implements Logger, FileWriter { public function log(string $msg): void { echo "Log: $msg\n"; } public function write(string $data): void { echo "Write: $data\n"; } }