0
0
PHPprogramming~20 mins

Intersection types in practice 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
Output of intersection type function call

What is the output of this PHP code using intersection types?

PHP
<?php
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 ): string {
    return $obj->foo() . '-' . $obj->bar();
}
$c = new C();
echo test($c);
?>
ASyntaxError
Bfoobar
Cfoo-barbar
Dfoo-bar
Attempts:
2 left
💡 Hint

Remember that the function requires an object implementing both interfaces A and B.

Predict Output
intermediate
2:00remaining
Return type with intersection types

What will this PHP code output?

PHP
<?php
interface X { public function x(): string; }
interface Y { public function y(): string; }
class Z implements X, Y {
    public function x(): string { return 'X'; }
    public function y(): string { return 'Y'; }
}
function create(): X&Y {
    return new Z();
}
$obj = create();
echo $obj->x() . $obj->y();
?>
AXY
BX Y
CSyntaxError
DFatal error: Return type mismatch
Attempts:
2 left
💡 Hint

The function returns an object implementing both interfaces X and Y.

🔧 Debug
advanced
2:00remaining
Identify the error with intersection types

What error will this PHP code produce?

PHP
<?php
interface M { public function m(): string; }
interface N { public function n(): string; }
class P implements M {
    public function m(): string { return 'm'; }
}
function callMN(M&N $obj): string {
    return $obj->m() . $obj->n();
}
$p = new P();
echo callMN($p);
?>
AFatal error: Uncaught TypeError: Argument 1 passed to callMN() must implement interface N
BSyntaxError
Cmnull
DCall to undefined method P::n()
Attempts:
2 left
💡 Hint

Check if the object passed implements both interfaces M and N.

📝 Syntax
advanced
2:00remaining
Syntax error with intersection types

Which option shows the correct syntax to declare a function parameter with intersection types in PHP?

Afunction example(A && B $param): void {}
Bfunction example(A & B $param): void {}
Cfunction example(A&B $param): void {}
Dfunction example(A | B $param): void {}
Attempts:
2 left
💡 Hint

Intersection types use a single ampersand without spaces.

🚀 Application
expert
3:00remaining
Using intersection types with traits and interfaces

Given the code below, what will be the output?

PHP
<?php
interface I1 { public function get(): string; }
interface I2 { public function get(): string; }
trait T1 { public function get(): string { return 'Trait'; } }
class MyClass implements I1, I2 {
    use T1;
}
function show(I1&I2 $obj): string {
    return $obj->get();
}
$instance = new MyClass();
echo show($instance);
?>
AInterface method get() must be public
BTrait
CSyntaxError
DFatal error: Class MyClass must implement method get()
Attempts:
2 left
💡 Hint

Traits can provide method implementations for interfaces.