Bird
Raised Fist0
PHPprogramming~20 mins

Intersection types 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
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.

Practice

(1/5)
1. What does an intersection type in PHP represent?
function example(SomeType&OtherType $param) {}
easy
A. A type that requires the value to satisfy both SomeType and OtherType.
B. A type that accepts either SomeType or OtherType.
C. A type that only accepts SomeType but ignores OtherType.
D. A type that combines SomeType and OtherType into a new class.

Solution

  1. Step 1: Understand intersection type syntax

    In PHP, the & symbol between types means the value must satisfy all listed types simultaneously.
  2. Step 2: Interpret the function parameter

    The parameter type SomeType&OtherType means the argument must be both SomeType and OtherType at the same time.
  3. Final Answer:

    A type that requires the value to satisfy both SomeType and OtherType. -> Option A
  4. Quick Check:

    Intersection type = must satisfy all types [OK]
Hint: Remember & means AND, so all types must match [OK]
Common Mistakes:
  • Confusing & with | (union type)
  • Thinking it creates a new combined class
  • Assuming it accepts either type instead of both
2. Which of the following is the correct syntax for declaring an intersection type in a function parameter in PHP?
easy
A. function test(A & B $param) {}
B. function test(A | B $param) {}
C. function test(A, B $param) {}
D. function test(A && B $param) {}

Solution

  1. Step 1: Recall intersection type syntax

    PHP uses a single ampersand & to declare intersection types between multiple types.
  2. Step 2: Check each option

    function test(A & B $param) {} uses & correctly. function test(A | B $param) {} uses | which is union type. function test(A, B $param) {} uses comma which is invalid. function test(A && B $param) {} uses && which is not valid syntax.
  3. Final Answer:

    function test(A & B $param) {} -> Option A
  4. Quick Check:

    Intersection type uses & symbol [OK]
Hint: Use single & between types for intersection [OK]
Common Mistakes:
  • Using | instead of &
  • Using commas to separate types
  • Using && instead of &
3. What will be the output of the following PHP code?
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. TypeError at runtime
B. foo
C. bar
D. foo bar

Solution

  1. Step 1: Understand class and interfaces

    Class C implements both interfaces A and B, so it has methods foo() and bar().
  2. Step 2: Analyze function test parameter and call

    The function test requires an argument that satisfies both A and B (intersection type). Object $c satisfies both. The function echoes foo() and bar() results concatenated with a space.
  3. Final Answer:

    foo bar -> Option D
  4. Quick Check:

    Object satisfies both interfaces, prints both methods [OK]
Hint: Intersection means object must have all methods used [OK]
Common Mistakes:
  • Expecting only one method output
  • Thinking it causes a TypeError
  • Confusing intersection with union types
4. Identify the error in this PHP code using intersection types:
interface X { public function x(): void; }
interface Y { public function y(): void; }

class Z implements X {
public function x(): void {}
}

function foo(X & Y $param) {
$param->x();
$param->y();
}

$z = new Z();
foo($z);
medium
A. Function foo has wrong parameter syntax for intersection types.
B. Method y() is missing in function foo.
C. Class Z does not implement interface Y, so it cannot satisfy X & Y.
D. No error, code runs fine.

Solution

  1. Step 1: Check class Z implementation

    Class Z implements only interface X, not Y, so it does not satisfy the intersection type X & Y.
  2. Step 2: Analyze function call with $z

    Function foo requires a parameter that implements both X and Y. Passing $z causes a TypeError because $z lacks Y.
  3. Final Answer:

    Class Z does not implement interface Y, so it cannot satisfy X & Y. -> Option C
  4. Quick Check:

    Intersection requires all interfaces implemented [OK]
Hint: Check all interfaces are implemented for intersection [OK]
Common Mistakes:
  • Assuming partial interface implementation is enough
  • Thinking syntax is wrong instead of logic
  • Ignoring missing methods from interfaces
5. Given these interfaces and classes:
interface Logger { public function log(string $msg): void; }
interface FileWriter { public function write(string $data): void; }

class FileLogger implements Logger, FileWriter {
public function log(string $msg): void { echo "Log: $msg\n"; }
public function write(string $data): void { echo "Write: $data\n"; }
}

function process(Logger & FileWriter $obj, string $message) {
$obj->log($message);
$obj->write($message);
}

$fl = new FileLogger();
process($fl, "Hello World");

What will be the output and why is intersection type useful here?
hard
A. Runtime error because intersection types cannot be used with classes.
B. Output:
Log: Hello World
Write: Hello World
Intersection ensures $obj has both log() and write() methods.
C. Output:
Log: Hello World
Only log() is called because intersection ignores write().
D. Output:
Write: Hello World
Only write() is called because log() is ignored.

Solution

  1. Step 1: Understand class and interfaces

    FileLogger implements both Logger and FileWriter, so it has both log() and write() methods.
  2. Step 2: Analyze function process behavior

    The function requires an object that satisfies both Logger and FileWriter (intersection type). It calls both log() and write() methods on the object.
  3. Step 3: Predict output

    Calling process with $fl outputs:
    Log: Hello World
    Write: Hello World
  4. Final Answer:

    Output:
    Log: Hello World
    Write: Hello World
    Intersection ensures $obj has both log() and write() methods.
    -> Option B
  5. Quick Check:

    Intersection type ensures all required methods exist [OK]
Hint: Intersection guarantees all methods exist on object [OK]
Common Mistakes:
  • Thinking only one interface method is called
  • Believing intersection types cause runtime errors
  • Ignoring that both methods are called
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