Interface declaration and implementation in PHP - Time & Space Complexity
When using interfaces in PHP, it's important to know how the program's running time changes as the code works with different objects.
We want to see how the time cost grows when calling methods defined by an interface.
Analyze the time complexity of the following code snippet.
interface Logger {
public function log(string $message): void;
}
class FileLogger implements Logger {
public function log(string $message): void {
// Simulate writing to a file
file_put_contents('log.txt', $message . PHP_EOL, FILE_APPEND);
}
}
$logger = new FileLogger();
for ($i = 0; $i < $n; $i++) {
$logger->log("Message $i");
}
This code defines an interface and a class that implements it. Then it calls the log method inside a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the
logmethod inside the loop. - How many times: The loop runs
ntimes, sologis calledntimes.
Each time we increase n, the number of log calls grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to log |
| 100 | 100 calls to log |
| 1000 | 1000 calls to log |
Pattern observation: The number of operations grows directly with n. Double n, double the calls.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of times we call the method.
[X] Wrong: "Using an interface makes the code slower in a way that changes the time complexity."
[OK] Correct: Calling methods through an interface adds a tiny fixed cost, but it does not change how the total time grows with input size.
Understanding how interfaces affect time complexity helps you explain your design choices clearly and shows you know how code structure relates to performance.
"What if the log method itself contained a loop that runs m times? How would the time complexity change?"
Practice
interface in PHP?Solution
Step 1: Understand what an interface is
An interface in PHP only declares method names without any code inside them.Step 2: Understand the purpose of interfaces
Interfaces ensure that any class implementing them must write the declared methods, helping organize code.Final Answer:
To define method names without their code, ensuring classes implement them -> Option CQuick Check:
Interface purpose = method declaration only [OK]
- Thinking interfaces store variables
- Believing interfaces create objects
- Confusing interfaces with classes
Vehicle in PHP?Solution
Step 1: Recall interface method declaration rules
In interfaces, methods are declared without code blocks (no curly braces with code).Step 2: Check each option
'interface Vehicle { public function start(); }' declares interface and method without code, which is correct. Declarations with method bodies '{}' are wrong. Using 'class' instead of 'interface' is incorrect.Final Answer:
interface Vehicle { public function start(); } -> Option BQuick Check:
Interface methods have no body [OK]
- Adding method bodies inside interfaces
- Using class keyword instead of interface
- Omitting semicolon after method declaration
interface Animal {
public function sound();
}
class Dog implements Animal {
public function sound() {
return "Bark";
}
}
$dog = new Dog();
echo $dog->sound();Solution
Step 1: Understand interface and class implementation
The interface Animal declares method sound(). Dog class implements Animal and defines sound() returning "Bark".Step 2: Check the output of calling $dog->sound()
Calling $dog->sound() returns "Bark" which is then echoed.Final Answer:
Bark -> Option AQuick Check:
Implemented method returns "Bark" [OK]
- Expecting interface name as output
- Confusing method name with output
- Thinking code causes error without implementation
interface Shape {
public function area();
}
class Circle implements Shape {
public function area() {
return 3.14 * 5 * 5;
}
public function perimeter() {
return 2 * 3.14 * 5;
}
}
$circle = new Circle();
echo $circle->area();Solution
Step 1: Check interface and class methods
Interface Shape declares area(). Circle implements area() correctly and adds extra method perimeter() which is allowed.Step 2: Verify if code runs without error
Since all interface methods are implemented, extra methods are allowed. Code outputs area value.Final Answer:
No error, code runs and outputs area -> Option AQuick Check:
Extra methods allowed, interface methods implemented [OK]
- Thinking extra methods must be in interface
- Confusing method body rules in interface
- Assuming missing interface methods cause error here
interface Printer {
public function printDocument();
}
interface Scanner {
public function scanDocument();
}How can you create a class
AllInOne that implements both interfaces and provides method bodies?Solution
Step 1: Understand multiple interface implementation
PHP allows a class to implement multiple interfaces by listing them separated by commas.Step 2: Check which option correctly implements both interfaces
class AllInOne implements Printer, Scanner { public function printDocument() { return "Printing..."; } public function scanDocument() { return "Scanning..."; } } implements both Printer and Scanner and defines both required methods. Others miss one interface or do not implement any.Final Answer:
class AllInOne implements Printer, Scanner { public function printDocument() { return "Printing..."; } public function scanDocument() { return "Scanning..."; } } -> Option DQuick Check:
Multiple interfaces implemented with commas [OK]
- Implementing only one interface when multiple required
- Forgetting to implement all interface methods
- Not using commas to separate interfaces
Ready to prove it?
Pass the test and this pattern moves unknown
