Abstract classes and methods in PHP - Time & Space Complexity
We want to understand how the time it takes to run code with abstract classes and methods changes as the program grows.
Specifically, we ask: how does using abstract classes affect the number of steps the program takes?
Analyze the time complexity of the following code snippet.
abstract class Animal {
abstract public function makeSound();
}
class Dog extends Animal {
public function makeSound() {
echo "Bark\n";
}
}
$dogs = [];
for ($i = 0; $i < 5; $i++) {
$dogs[] = new Dog();
}
foreach ($dogs as $dog) {
$dog->makeSound();
}
This code defines an abstract class with an abstract method, then creates several objects from a subclass and calls the method on each.
- Primary operation: Looping through the array of Dog objects and calling
makeSound(). - How many times: The loop runs once for each Dog object, here 5 times.
As the number of Dog objects increases, the number of times makeSound() is called grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to makeSound() |
| 100 | 100 calls to makeSound() |
| 1000 | 1000 calls to makeSound() |
Pattern observation: The number of operations grows directly with the number of objects.
Time Complexity: O(n)
This means the time to run the method calls grows in a straight line as you add more objects.
[X] Wrong: "Using abstract classes makes the program slower because of extra overhead."
[OK] Correct: The abstract class itself does not add loops or repeated work; the time depends on how many objects and method calls you have, not on abstraction.
Understanding how abstract classes affect performance helps you explain design choices clearly and shows you know how code structure relates to speed.
"What if we added a nested loop inside makeSound() that runs n times? How would the time complexity change?"
Practice
abstract class in PHP?Solution
Step 1: Understand abstract class concept
An abstract class is designed to be a base class that other classes extend but cannot be instantiated on its own.Step 2: Eliminate incorrect options
Options A, B, and D do not describe abstract classes correctly. PHP does not support multiple inheritance, and abstract classes cannot be instantiated directly.Final Answer:
To provide a base class that cannot be instantiated directly -> Option DQuick Check:
Abstract class = base class no direct objects [OK]
- Thinking abstract classes can be instantiated
- Confusing abstract classes with interfaces
- Believing abstract classes store data only
Solution
Step 1: Recall abstract method syntax
In PHP, abstract methods are declared with the keywordabstractbefore the function signature and have no body (no curly braces).Step 2: Check each option
abstract public function myMethod(); correctly declares an abstract method:abstract public function myMethod();. Options B and D have bodies or wrong order, and C uses incorrect syntax.Final Answer:
abstract public function myMethod(); -> Option AQuick Check:
Abstract method = abstract + no body [OK]
- Adding curly braces {} to abstract methods
- Wrong keyword order or missing 'abstract'
- Trying to implement method body in abstract method
abstract class Animal {
abstract public function sound();
}
class Dog extends Animal {
public function sound() {
return "Bark";
}
}
$dog = new Dog();
echo $dog->sound();Solution
Step 1: Understand class inheritance and method implementation
The abstract classAnimalhas an abstract methodsound(). The classDogextendsAnimaland implementssound()returning "Bark".Step 2: Check object creation and method call
We create an object ofDog(which is allowed) and callsound(). This returns "Bark" which is echoed.Final Answer:
Bark -> Option CQuick Check:
Implemented abstract method returns 'Bark' [OK]
- Trying to instantiate abstract class directly
- Forgetting to implement abstract method in child
- Expecting syntax error due to abstract method
abstract class Vehicle {
abstract public function startEngine();
}
class Car extends Vehicle {
// No implementation of startEngine()
}
$car = new Car();
$car->startEngine();Solution
Step 1: Check abstract method implementation
The abstract classVehicledeclares an abstract methodstartEngine(). The child classCardoes not implement this method.Step 2: Understand PHP rules for abstract methods
PHP requires all abstract methods to be implemented in child classes unless the child is also abstract. SinceCaris concrete and missing the method, this causes an error.Final Answer:
Class Car must implement abstract method startEngine() -> Option AQuick Check:
Missing abstract method implementation = error [OK]
- Forgetting to implement abstract methods in child
- Trying to instantiate abstract class directly
- Assuming abstract methods can have bodies
abstract class Shape {
abstract public function area();
}
class Square extends Shape {
private $side;
public function __construct($side) {
$this->side = $side;
}
public function area() {
return $this->side * $this->side;
}
}
class Circle extends Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function area() {
return 3.14 * $this->radius * $this->radius;
}
}
$shapes = [new Square(4), new Circle(3)];
foreach ($shapes as $shape) {
echo $shape->area() . " ";
}Solution
Step 1: Calculate area for Square
Square side is 4, so area = 4 * 4 = 16.Step 2: Calculate area for Circle
Circle radius is 3, so area = 3.14 * 3 * 3 = 28.26.Step 3: Understand output formatting
The code echoes each area followed by a space, so output is "16 28.26 " without newline.Final Answer:
16 28.26 -> Option BQuick Check:
Area calculations correct, output matches [OK]
- Confusing output formatting with newline
- Trying to instantiate abstract class Shape
- Incorrect area formulas
Ready to prove it?
Pass the test and this pattern moves unknown
