Multiple interface implementation in PHP - Time & Space Complexity
When a class implements multiple interfaces, it must provide all the methods those interfaces require.
We want to understand how this affects the time it takes to run the program.
Analyze the time complexity of the following code snippet.
interface A {
public function methodA();
}
interface B {
public function methodB();
}
class MyClass implements A, B {
public function methodA() {
// simple operation
}
public function methodB() {
// simple operation
}
}
$obj = new MyClass();
$obj->methodA();
$obj->methodB();
This code shows a class implementing two interfaces and calling their methods.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling methods defined by interfaces.
- How many times: Each method is called once here, no loops or recursion.
Since there are no loops or repeated calls, the time grows very little as input size grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 method calls |
| 100 | 2 method calls |
| 1000 | 2 method calls |
Pattern observation: The number of operations stays the same regardless of input size.
Time Complexity: O(1)
This means the time to run does not grow with input size; it stays constant.
[X] Wrong: "Implementing more interfaces makes the program slower as input grows."
[OK] Correct: The number of interfaces does not affect how many times methods run unless loops or recursion are involved.
Understanding how interface implementation affects performance helps you explain design choices clearly and confidently.
"What if each method called inside the class had a loop over an input array? How would the time complexity change?"
Practice
Solution
Step 1: Understand interface implementation
When a class implements interfaces, it promises to include all methods declared in those interfaces.Step 2: Multiple interfaces separated by commas
Listing interfaces separated by commas means the class must implement all methods from each interface.Final Answer:
The class promises to have all methods defined in all those interfaces. -> Option DQuick Check:
Multiple interfaces = all methods implemented [OK]
- Thinking interfaces provide properties
- Confusing interfaces with class inheritance
- Believing methods are auto-created
FirstInterface and SecondInterface?Solution
Step 1: Identify correct keyword for interfaces
PHP uses the keywordimplementsto indicate a class uses interfaces.Step 2: Check syntax for multiple interfaces
Multiple interfaces are listed separated by commas afterimplements.Final Answer:
class MyClass implements FirstInterface, SecondInterface {} -> Option BQuick Check:
implements + comma-separated interfaces = correct syntax [OK]
- Using 'extends' instead of 'implements' for interfaces
- Missing commas between interface names
- Using 'uses' keyword incorrectly
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();Solution
Step 1: Check interface methods implemented
The class Test implements both interfaces A and B and defines both methods foo() and bar() correctly.Step 2: Understand output of method calls
Calling foo() returns 'foo' and bar() returns 'bar'. Echo concatenates them with a space.Final Answer:
foo bar -> Option AQuick Check:
Implemented methods return strings combined with space [OK]
- Forgetting to implement all interface methods
- Missing space in echo output
- Expecting concatenation without space
interface X { public function methodX(); }
interface Y { public function methodY(); }
class Demo implements X, Y {
public function methodX() { return 'X'; }
}Solution
Step 1: Check implemented interfaces and methods
Class Demo implements interfaces X and Y, so it must define all methods from both.Step 2: Identify missing method
methodY() from interface Y is not implemented, causing an error.Final Answer:
Class Demo must implement methodY() from interface Y. -> Option CQuick Check:
All interface methods must be implemented [OK]
- Implementing only some interface methods
- Confusing extends with implements
- Assuming interfaces can be partially implemented
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?
Solution
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.Step 2: Understand print_r output
print_r outputs the logs array with 'Start' and 'End' entries after the echoed notify message.Final Answer:
Notify: Alert Array ( [0] => Start [1] => End ) -> Option AQuick Check:
Echo notify first, then print logs array [OK]
- Assuming print_r output appears before echo
- Confusing array contents logged
- Expecting syntax errors without missing methods
Ready to prove it?
Pass the test and this pattern moves unknown
