Challenge - 5 Problems
Intersection Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code using intersection types?
Consider the following PHP code that uses intersection types in a function parameter. What will be the output when calling
process(new A_B())?PHP
<?php interface A { public function foo(): string; } interface B { public function bar(): string; } class A_B implements A, B { public function foo(): string { return "foo"; } public function bar(): string { return "bar"; } } function process(A&B $obj): string { return $obj->foo() . "-" . $obj->bar(); } echo process(new A_B());
Attempts:
2 left
💡 Hint
Remember that intersection types require the object to implement all interfaces listed.
✗ Incorrect
The function
process requires an object that implements both interfaces A and B. The class A_B implements both, so the method calls foo() and bar() return "foo" and "bar" respectively, joined by a dash.❓ Predict Output
intermediate2:00remaining
What error does this PHP code raise with intersection types?
What error will this PHP code produce when calling
process(new C())?PHP
<?php interface A { public function foo(): string; } interface B { public function bar(): string; } class C implements A { public function foo(): string { return "foo"; } } function process(A&B $obj): string { return $obj->foo() . "-" . $obj->bar(); } echo process(new C());
Attempts:
2 left
💡 Hint
Check if the object implements all interfaces required by the intersection type.
✗ Incorrect
The function
process requires an object implementing both A and B. Class C only implements A, so passing it causes a TypeError before the function body runs.🔧 Debug
advanced2:00remaining
Why does this PHP code with intersection types cause a fatal error?
This code intends to accept an object that implements both interfaces
X and Y. Why does it cause a fatal error when calling handle(new Z())?PHP
<?php interface X { public function x(): string; } interface Y { public function y(): string; } class Z implements X { public function x(): string { return "X"; } } function handle(X&Y $obj): string { return $obj->x() . $obj->y(); } echo handle(new Z());
Attempts:
2 left
💡 Hint
Intersection types require the object to implement all interfaces listed.
✗ Incorrect
The function
handle requires an object implementing both X and Y. Class Z only implements X, so passing it causes a TypeError before the function runs.🚀 Application
advanced2:00remaining
Which class can be passed to this function with intersection types?
Given this function that requires an object implementing both
Readable and Writable interfaces, which class can be passed without error?PHP
<?php interface Readable { public function read(): string; } interface Writable { public function write(string $data): void; } function operate(Readable&Writable $obj): string { $obj->write("data"); return $obj->read(); }
Attempts:
2 left
💡 Hint
The object must implement both interfaces to satisfy the intersection type.
✗ Incorrect
Only class B implements both
Readable and Writable, so it can be passed to operate without error.🧠 Conceptual
expert2:00remaining
What is the main purpose of intersection types in PHP?
Choose the best description of what intersection types allow you to do in PHP.
Attempts:
2 left
💡 Hint
Think about how intersection types combine requirements.
✗ Incorrect
Intersection types require a value to satisfy all listed types at once, for example, an object implementing multiple interfaces simultaneously.