Extending classes in PHP - Time & Space Complexity
When we extend classes in PHP, we add new features to existing code. It's important to see how this affects the time it takes for the program to run.
We want to know: how does adding a child class change the work the program does?
Analyze the time complexity of the following code snippet.
class ParentClass {
public function process(array $items) {
foreach ($items as $item) {
echo $item . "\n";
}
}
}
class ChildClass extends ParentClass {
public function process(array $items) {
parent::process($items);
echo "Done processing " . count($items) . " items.";
}
}
$child = new ChildClass();
$child->process([1, 2, 3, 4, 5]);
This code defines a parent class with a method that prints each item in an array. The child class extends it by calling the parent method and then printing a summary message.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
foreachloop that goes through each item in the array. - How many times: Once for each item in the input array.
As the number of items grows, the loop runs more times, printing each item once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The work grows directly with the number of items. Double the items, double the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of items processed.
[X] Wrong: "Extending a class always makes the program slower by a lot."
[OK] Correct: Extending a class just adds a little extra work, like printing a summary. The main loop still runs once per item, so the overall time grows the same way.
Understanding how extending classes affects time helps you write clear and efficient code. It shows you can think about both design and performance, a skill many developers value.
"What if the child class added another loop over the items? How would the time complexity change?"
Practice
Solution
Step 1: Understand class inheritance in PHP
PHP uses a specific keyword to let one class inherit properties and methods from another.Step 2: Identify the correct keyword
The keyword to extend a class in PHP isextends, notinherits,implements, oruses.Final Answer:
extends -> Option AQuick Check:
Keyword for inheritance = extends [OK]
- Using 'inherits' instead of 'extends'
- Confusing 'implements' with 'extends'
- Using 'uses' keyword incorrectly
Animal in PHP?Solution
Step 1: Recall PHP class extension syntax
To create a child class, PHP uses the syntax:class Child extends Parent {}.Step 2: Match the syntax with options
class Dog extends Animal {} matches the correct syntax. The other options use incorrect keywords.Final Answer:
class Dog extends Animal {} -> Option AQuick Check:
Correct syntax uses 'extends' keyword [OK]
- Using 'inherits' instead of 'extends'
- Confusing 'implements' with 'extends'
- Using 'use' keyword for inheritance
class ParentClass {
public function greet() {
return "Hello from Parent";
}
}
class ChildClass extends ParentClass {
public function greet() {
return "Hello from Child";
}
}
$object = new ChildClass();
echo $object->greet();Solution
Step 1: Understand method overriding in child class
The child classChildClassoverrides thegreet()method of the parent class.Step 2: Determine which method is called
When callinggreet()on an instance ofChildClass, the overridden method in the child class runs.Final Answer:
Hello from Child -> Option CQuick Check:
Child method overrides parent method [OK]
- Assuming parent method runs instead of child
- Expecting syntax error due to override
- Thinking no output will be printed
class Vehicle {
public function start() {
return "Starting vehicle";
}
}
class Car Vehicle {
public function start() {
return "Starting car";
}
}Solution
Step 1: Check class extension syntax
In PHP, to extend a class, the child class must use theextendskeyword.Step 2: Identify the missing keyword
The codeclass Car Vehicle {}missesextendsbetweenCarandVehicle.Final Answer:
Missing 'extends' keyword before Vehicle -> Option DQuick Check:
Extending requires 'extends' keyword [OK]
- Omitting 'extends' keyword
- Thinking methods cannot be overridden
- Confusing abstract class requirement
class Base {
public function message() {
return "Base message";
}
}
class Derived extends Base {
public function message() {
return parent::message() . " and Derived message";
}
}
class MoreDerived extends Derived {
public function message() {
return parent::message() . " plus MoreDerived message";
}
}
$obj = new MoreDerived();
echo $obj->message();Solution
Step 1: Trace method calls through inheritance chain
MoreDerived::message()callsparent::message(), which isDerived::message().Step 2: Understand Derived::message() call
Derived::message()callsparent::message(), which isBase::message(), returning "Base message".Step 3: Combine returned strings
Base returns "Base message"; Derived appends " and Derived message"; MoreDerived appends " plus MoreDerived message".Final Answer:
Base message and Derived message plus MoreDerived message -> Option BQuick Check:
parent:: calls chain methods up inheritance [OK]
- Ignoring parent:: calls and output only last method
- Expecting fatal error from parent calls
- Thinking only base method runs
Ready to prove it?
Pass the test and this pattern moves unknown
