0
0
PHPprogramming~20 mins

Intersection types in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Intersection Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
Afoo-barbar
Bfoobar
Cfoo-bar
DFatal error: Uncaught TypeError
Attempts:
2 left
💡 Hint
Remember that intersection types require the object to implement all interfaces listed.
Predict Output
intermediate
2: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());
AFatal error: Uncaught Error: Call to undefined method C::bar()
Bfoo-bar
Cfoo-
DFatal error: Uncaught TypeError: Argument 1 passed to process() must implement interface B
Attempts:
2 left
💡 Hint
Check if the object implements all interfaces required by the intersection type.
🔧 Debug
advanced
2: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());
AFatal error: Uncaught TypeError because Z does not implement interface Y
BFatal error: Call to undefined method Z::y()
COutput: XY
DOutput: X
Attempts:
2 left
💡 Hint
Intersection types require the object to implement all interfaces listed.
🚀 Application
advanced
2: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();
}
Aclass File implements Readable { public function read(): string { return "file"; } }
Bclass File implements Readable, Writable { public function read(): string { return "file"; } public function write(string $data): void {} }
Cclass File implements Writable { public function write(string $data): void {} }
Dclass File {}
Attempts:
2 left
💡 Hint
The object must implement both interfaces to satisfy the intersection type.
🧠 Conceptual
expert
2: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.
ARequire a value to satisfy multiple type constraints simultaneously, such as implementing multiple interfaces.
BAllow a value to be one of several types, like a union type.
CDefine a new type that inherits properties from multiple classes.
DAutomatically convert types during function calls.
Attempts:
2 left
💡 Hint
Think about how intersection types combine requirements.