Bird
Raised Fist0
PHPprogramming~20 mins

Intersection types in practice in PHP - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does an intersection type like A&B mean in PHP?
easy
A. The value must be type B only.
B. The value can be either type A or type B.
C. The value must satisfy both type A and type B simultaneously.
D. The value must be type A only.

Solution

  1. Step 1: Understand intersection type concept

    Intersection types require a value to meet all listed types at once, not just one.
  2. Step 2: Apply to A&B

    The value must be both type A and type B simultaneously.
  3. Final Answer:

    The value must satisfy both type A and type B simultaneously. -> Option C
  4. Quick Check:

    Intersection means AND, so both types required [OK]
Hint: Intersection means all types must be met together [OK]
Common Mistakes:
  • Confusing intersection (&) with union (|)
  • Thinking it means either type A or B
  • Assuming it means only one type
2. Which of the following is the correct syntax for declaring a function parameter with an intersection type of interfaces Countable and Iterator?
easy
A. function process(Countable&Iterator $obj) {}
B. function process(Countable|Iterator $obj) {}
C. function process(Countable, Iterator $obj) {}
D. function process(Countable & Iterator $obj) {}

Solution

  1. Step 1: Recall intersection type syntax in PHP

    PHP uses the ampersand (&) without spaces and escaped as & in HTML to declare intersection types.
  2. Step 2: Check each option

    function process(Countable&Iterator $obj) {} uses Countable&Iterator which is correct syntax for intersection types in PHP.
  3. Final Answer:

    function process(Countable&Iterator $obj) {} -> Option A
  4. Quick Check:

    Intersection uses & without spaces [OK]
Hint: Use & with no spaces for intersection types [OK]
Common Mistakes:
  • Using | instead of & for intersection
  • Adding spaces around &
  • Using comma instead of &
3. Given the following code, what will be the output?
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. foo bar
B. foo
C. bar
D. TypeError at runtime

Solution

  1. Step 1: Understand class C implements both interfaces

    Class C implements both A and B, so it has methods foo() and bar().
  2. 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.
  3. Final Answer:

    foo bar -> Option A
  4. 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

  1. Step 1: Check class Z interfaces

    Class Z implements only interface X, not Y.
  2. Step 2: Analyze function parameter type

    Function example requires an object implementing both X and Y (intersection). Passing $z violates this.
  3. Final Answer:

    TypeError because $z does not implement interface Y. -> Option B
  4. 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

  1. Step 1: Understand intersection type usage

    Use & to require both JsonSerializable and Countable interfaces.
  2. Step 2: Handle custom method not in interfaces

    Since customMethod() is not part of these interfaces, check its existence with method_exists before calling.
  3. Final Answer:

    function process(JsonSerializable&Countable $obj) { if (method_exists($obj, 'customMethod')) { $obj->customMethod(); } } -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard