Method overriding in PHP - Time & Space Complexity
We want to understand how the time it takes to run a program changes when we use method overriding in PHP.
Specifically, does replacing a method in a child class affect how long the program runs as it gets bigger?
Analyze the time complexity of the following code snippet.
class Animal {
public function speak() {
echo "Animal sound\n";
}
}
class Dog extends Animal {
public function speak() {
echo "Bark\n";
}
}
$dog = new Dog();
$dog->speak();
This code shows a child class overriding a method from its parent class and then calling it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the overridden method
speak()once. - How many times: Exactly one time in this example.
Since the method is called only once, the time to run does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 method call |
| 100 | 1 method call |
| 1000 | 1 method call |
Pattern observation: The number of operations stays the same no matter how big the input is.
Time Complexity: O(1)
This means the time to run the method does not change as the input size grows.
[X] Wrong: "Overriding a method makes the program slower as input grows because it adds extra steps."
[OK] Correct: Overriding just replaces the method code; it does not add loops or repeated work by itself.
Understanding how method overriding affects performance helps you explain your code design clearly and shows you know how programs run under the hood.
"What if the overridden method contained a loop that runs n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand method overriding concept
Method overriding means a child class uses the same method name as the parent to change its behavior.Step 2: Identify what overriding does
It replaces or modifies the parent's method behavior in the child class.Final Answer:
Change the behavior of a method inherited from the parent class -> Option AQuick Check:
Method overriding = change parent method behavior [OK]
- Thinking overriding creates new methods with different names
- Confusing overriding with inheritance itself
- Believing overriding prevents parent instantiation
greet in a child class in PHP?Solution
Step 1: Recall method overriding syntax
To override, define a method with the same name in the child class.Step 2: Check options for correct syntax
Only function greet() { /* new code */ } definesfunction greet()correctly without extra keywords.Final Answer:
function greet() { /* new code */ } -> Option BQuick Check:
Same method name in child = override [OK]
- Adding 'override' keyword which PHP does not use
- Changing method name to something else
- Trying to call parent method syntax incorrectly
class ParentClass {
function sayHello() {
echo "Hello from Parent";
}
}
class ChildClass extends ParentClass {
function sayHello() {
echo "Hello from Child";
}
}
$obj = new ChildClass();
$obj->sayHello();Solution
Step 1: Identify method overriding in ChildClass
ChildClass definessayHello()which overrides ParentClass method.Step 2: Check which method is called
Creating ChildClass object calls ChildClass'ssayHello(), printing "Hello from Child".Final Answer:
Hello from Child -> Option CQuick Check:
Child method overrides parent = "Hello from Child" [OK]
- Expecting parent method output instead of child
- Thinking both outputs print together
- Assuming error due to method name clash
class Base {
function show() {
echo "Base";
}
}
class Derived extends Base {
function show() {
parent.show();
echo " Derived";
}
}
$obj = new Derived();
$obj->show();Solution
Step 1: Identify how parent method is called
The code usesparent.show()which is invalid syntax in PHP.Step 2: Correct syntax for calling parent method
PHP requiresparent::show()to call the parent's method inside child.Final Answer:
Incorrect syntax to call parent method; should use parent::show() -> Option DQuick Check:
Call parent method with parent::method() [OK]
- Using dot (.) instead of :: to call parent method
- Thinking overriding is not allowed
- Forgetting to call parent method when needed
class Animal {
function sound() {
echo "Some sound";
}
}
class Dog extends Animal {
function sound() {
parent::sound();
echo " and Bark";
}
}
class Puppy extends Dog {
function sound() {
echo "Yip";
}
}
$pet = new Puppy();
$pet->sound();Solution
Step 1: Trace method calls for Puppy object
Puppy overridessound()and only echoes "Yip" without calling parent.Step 2: Understand overriding chain
Since Puppy method does not callparent::sound(), Dog and Animal methods are not executed.Final Answer:
Yip -> Option AQuick Check:
Child override without parent call = only child output [OK]
- Assuming all parent methods run automatically
- Expecting combined outputs without explicit parent calls
- Thinking multiple overrides cause errors
Ready to prove it?
Pass the test and this pattern moves unknown
