0
0
PHPprogramming~20 mins

Multiple interface implementation in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of multiple interface implementation
What is the output of this PHP code that implements two interfaces in one class?
PHP
<?php
interface A {
    public function foo();
}
interface B {
    public function bar();
}
class C implements A, B {
    public function foo() {
        echo "foo called\n";
    }
    public function bar() {
        echo "bar called\n";
    }
}
$obj = new C();
$obj->foo();
$obj->bar();
?>
A
foo called
bar called
B
foo called
C
bar called
DFatal error: Class C must implement method bar()
Attempts:
2 left
💡 Hint
Remember that a class implementing multiple interfaces must define all their methods.
Predict Output
intermediate
2:00remaining
Which method is called from which interface?
Given this PHP code with two interfaces and a class implementing both, what is the output?
PHP
<?php
interface X {
    public function test();
}
interface Y {
    public function test();
}
class Z implements X, Y {
    public function test() {
        echo "test method from Z\n";
    }
}
$z = new Z();
$z->test();
?>
A
test method from Z
BFatal error: Class Z must implement method test() from interface Y
C
test method from X
DNo output
Attempts:
2 left
💡 Hint
One method can satisfy multiple interfaces if signatures match.
Predict Output
advanced
2:00remaining
Output when interface methods have different signatures
What happens when a class tries to implement two interfaces with methods of the same name but different parameters?
PHP
<?php
interface I1 {
    public function run(int $a);
}
interface I2 {
    public function run(string $b);
}
class Test implements I1, I2 {
    public function run($param) {
        echo gettype($param) . "\n";
    }
}
$test = new Test();
$test->run(5);
$test->run("hello");
?>
A
string
string
B
integer
integer
C
integer
string
DFatal error: Declaration of Test::run() must be compatible with both interfaces
Attempts:
2 left
💡 Hint
PHP requires method signatures to be compatible when implementing multiple interfaces.
Predict Output
advanced
2:00remaining
Output of interface constants in multiple interfaces
What is the output of this PHP code using constants from multiple interfaces implemented by one class?
PHP
<?php
interface First {
    const VALUE = 10;
}
interface Second {
    const VALUE = 20;
}
class Demo implements First, Second {
    public function show() {
        echo self::VALUE . "\n";
    }
}
$obj = new Demo();
$obj->show();
?>
A
0
B
20
CFatal error: Demo::VALUE is ambiguous
D
10
Attempts:
2 left
💡 Hint
When interfaces have constants with the same name, the class must resolve ambiguity.
🧠 Conceptual
expert
2:00remaining
Why must a class implement all interface methods?
In PHP, why does a class that implements multiple interfaces have to define all methods declared in those interfaces?
ABecause PHP automatically provides default implementations for interface methods.
BBecause interfaces define a contract that the class must fulfill to be considered valid.
CBecause interfaces are optional and do not require method definitions.
DBecause classes can only implement one interface at a time.
Attempts:
2 left
💡 Hint
Think about what interfaces represent in object-oriented programming.