Step 1: Understand class C implements both interfaces
Class C implements both A and B, so it has methods foo() and bar().
Step 2: Call test with intersection type parameter
Function test requires an object that is both A and B, which $c is. It calls foo() and bar() and echoes their results separated by space.
Final Answer:
foo bar -> Option A
Quick Check:
Object meets both interfaces, so output is 'foo bar' [OK]
Hint: Check if object implements all interfaces in intersection [OK]
Common Mistakes:
Expecting only one method output
Thinking intersection causes error if both interfaces implemented
Confusing union with intersection types
4. What is the error in this code snippet?
interface X { public function x(): void; } interface Y { public function y(): void; }
function example(X&Y $obj) { $obj->x(); $obj->y(); }
class Z implements X { public function x(): void {} }
$z = new Z(); example($z);
medium
A. No error, code runs fine.
B. TypeError because $z does not implement interface Y.
C. Syntax error in function declaration.
D. Fatal error: method y() not found in class Z.
Solution
Step 1: Check class Z interfaces
Class Z implements only interface X, not Y.
Step 2: Analyze function parameter type
Function example requires an object implementing both X and Y (intersection). Passing $z violates this.
Final Answer:
TypeError because $z does not implement interface Y. -> Option B
Quick Check:
Intersection requires all interfaces, missing one causes TypeError [OK]
Hint: All interfaces in intersection must be implemented [OK]
Common Mistakes:
Assuming partial interface implementation is enough
Confusing syntax error with type error
Thinking missing method causes fatal error instead of TypeError
5. You want to write a function that accepts an object which must be both JsonSerializable and Countable. You also want to ensure the object has a method customMethod(). Which of the following is the best way to type hint this in PHP 8.1+?
hard
A. function process(JsonSerializable|Countable $obj) { $obj->customMethod(); }
B. function process(JsonSerializable&Countable&CustomInterface $obj) {}
C. function process(object $obj) { $obj->customMethod(); }
D. function process(JsonSerializable&Countable $obj) { if (method_exists($obj, 'customMethod')) { $obj->customMethod(); } }
Solution
Step 1: Understand intersection type usage
Use & to require both JsonSerializable and Countable interfaces.
Step 2: Handle custom method not in interfaces
Since customMethod() is not part of these interfaces, check its existence with method_exists before calling.
Final Answer:
function process(JsonSerializable&Countable $obj) { if (method_exists($obj, 'customMethod')) { $obj->customMethod(); } } -> Option D
Quick Check:
Intersection + runtime check for extra method [OK]
Hint: Use & for interfaces and method_exists for extra methods [OK]
Common Mistakes:
Using union (|) instead of intersection (&)
Assuming method in interfaces without checking
Ignoring type safety by using object without interfaces
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.