Bird
Raised Fist0
PHPprogramming~20 mins

Multiple interface implementation in PHP - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What does it mean when a PHP class implements multiple interfaces separated by commas?
easy
A. The class can only use one interface at a time.
B. The class inherits properties from multiple parent classes.
C. The class automatically creates methods from the interfaces.
D. The class promises to have all methods defined in all those interfaces.

Solution

  1. Step 1: Understand interface implementation

    When a class implements interfaces, it promises to include all methods declared in those interfaces.
  2. Step 2: Multiple interfaces separated by commas

    Listing interfaces separated by commas means the class must implement all methods from each interface.
  3. Final Answer:

    The class promises to have all methods defined in all those interfaces. -> Option D
  4. Quick Check:

    Multiple interfaces = all methods implemented [OK]
Hint: Remember: commas mean all interfaces must be implemented [OK]
Common Mistakes:
  • Thinking interfaces provide properties
  • Confusing interfaces with class inheritance
  • Believing methods are auto-created
2. Which of the following is the correct syntax for a PHP class implementing two interfaces named FirstInterface and SecondInterface?
easy
A. class MyClass extends FirstInterface, SecondInterface {}
B. class MyClass implements FirstInterface, SecondInterface {}
C. class MyClass implements FirstInterface SecondInterface {}
D. class MyClass uses FirstInterface, SecondInterface {}

Solution

  1. Step 1: Identify correct keyword for interfaces

    PHP uses the keyword implements to indicate a class uses interfaces.
  2. Step 2: Check syntax for multiple interfaces

    Multiple interfaces are listed separated by commas after implements.
  3. Final Answer:

    class MyClass implements FirstInterface, SecondInterface {} -> Option B
  4. Quick Check:

    implements + comma-separated interfaces = correct syntax [OK]
Hint: Use 'implements' and commas to list interfaces [OK]
Common Mistakes:
  • Using 'extends' instead of 'implements' for interfaces
  • Missing commas between interface names
  • Using 'uses' keyword incorrectly
3. What will be the output of the following PHP code?
interface A { public function foo(); }
interface B { public function bar(); }
class Test implements A, B {
public function foo() { return 'foo'; }
public function bar() { return 'bar'; }
}
$obj = new Test();
echo $obj->foo() . ' ' . $obj->bar();
medium
A. foo bar
B. foobar
C. Syntax error
D. Runtime error

Solution

  1. Step 1: Check interface methods implemented

    The class Test implements both interfaces A and B and defines both methods foo() and bar() correctly.
  2. Step 2: Understand output of method calls

    Calling foo() returns 'foo' and bar() returns 'bar'. Echo concatenates them with a space.
  3. Final Answer:

    foo bar -> Option A
  4. Quick Check:

    Implemented methods return strings combined with space [OK]
Hint: Check all interface methods are implemented to avoid errors [OK]
Common Mistakes:
  • Forgetting to implement all interface methods
  • Missing space in echo output
  • Expecting concatenation without space
4. Identify the error in this PHP code:
interface X { public function methodX(); }
interface Y { public function methodY(); }
class Demo implements X, Y {
public function methodX() { return 'X'; }
}
medium
A. Interface Y cannot be implemented with interface X.
B. Class Demo should extend interfaces, not implement.
C. Class Demo must implement methodY() from interface Y.
D. No error, code is correct.

Solution

  1. Step 1: Check implemented interfaces and methods

    Class Demo implements interfaces X and Y, so it must define all methods from both.
  2. Step 2: Identify missing method

    methodY() from interface Y is not implemented, causing an error.
  3. Final Answer:

    Class Demo must implement methodY() from interface Y. -> Option C
  4. Quick Check:

    All interface methods must be implemented [OK]
Hint: Implement all methods from every interface you list [OK]
Common Mistakes:
  • Implementing only some interface methods
  • Confusing extends with implements
  • Assuming interfaces can be partially implemented
5. Given these interfaces and classes:
interface Logger { public function log(string $msg); }
interface Notifier { public function notify(string $msg); }
class System implements Logger, Notifier {
private array $logs = [];
public function log(string $msg) { $this->logs[] = $msg; }
public function notify(string $msg) { echo "Notify: $msg\n"; }
public function getLogs(): array { return $this->logs; }
}
$sys = new System();
$sys->log('Start');
$sys->notify('Alert');
$sys->log('End');
print_r($sys->getLogs());

What is the output of this code?
hard
A. Notify: Alert Array ( [0] => Start [1] => End )
B. Array ( [0] => Start [1] => End ) Notify: Alert
C. Notify: Alert Array ( [0] => Alert )
D. Syntax error due to missing interface methods

Solution

  1. Step 1: Trace method calls

    First, log('Start') adds 'Start' to logs array. Then notify('Alert') echoes 'Notify: Alert'. Then log('End') adds 'End' to logs.
  2. Step 2: Understand print_r output

    print_r outputs the logs array with 'Start' and 'End' entries after the echoed notify message.
  3. Final Answer:

    Notify: Alert Array ( [0] => Start [1] => End ) -> Option A
  4. Quick Check:

    Echo notify first, then print logs array [OK]
Hint: Echo outputs appear before print_r array display [OK]
Common Mistakes:
  • Assuming print_r output appears before echo
  • Confusing array contents logged
  • Expecting syntax errors without missing methods
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