Final classes and methods in PHP - Time & Space Complexity
Let's see how using final classes and methods affects how long a PHP program takes to run.
We want to know if marking classes or methods as final changes the speed as the program grows.
Analyze the time complexity of the following code snippet.
final class Calculator {
public function add(array $numbers): int {
$sum = 0;
foreach ($numbers as $num) {
$sum += $num;
}
return $sum;
}
}
$calc = new Calculator();
$result = $calc->add([1, 2, 3, 4, 5]);
This code defines a final class with a method that adds numbers in an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array to add numbers.
- How many times: Once for each number in the input array.
As the number of numbers grows, the time to add them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 additions |
| 100 | About 100 additions |
| 1000 | About 1000 additions |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to run grows in a straight line with the input size.
[X] Wrong: "Making a class or method final makes the code run faster."
[OK] Correct: Final only stops changes in code structure; it does not change how many steps the program takes.
Understanding time complexity helps you explain how your code behaves as it grows, which is a useful skill in many coding discussions.
"What if the add method used recursion instead of a loop? How would the time complexity change?"
Practice
final keyword do when used with a class in PHP?Solution
Step 1: Understand the role of
Thefinalwith classesfinalkeyword before a class means no other class can extend it.Step 2: Compare with other options
Options A, B, and D describe behaviors that are opposite or unrelated tofinalclasses.Final Answer:
Prevents the class from being extended by other classes -> Option AQuick Check:
final class = no extension allowed [OK]
- Thinking final class can be extended
- Confusing final with abstract
- Assuming final allows method overrides
Solution
Step 1: Recall correct final method syntax
In PHP, thefinalkeyword comes before the function keyword when declaring a final method.Step 2: Check each option
final function myMethod() {} matches the correct syntax. Options A, C, and D have incorrect order or placement of keywords.Final Answer:
final function myMethod() {} -> Option BQuick Check:
final before function keyword = correct syntax [OK]
- Placing final after function name
- Writing final after function keyword incorrectly
- Mixing order of keywords
final class Car {
final public function start() {
return "Engine started";
}
}
class SportsCar extends Car {
public function start() {
return "Sports engine started";
}
}
$car = new SportsCar();
echo $car->start();Solution
Step 1: Analyze class and method declarations
The classCaris declared asfinal, so it cannot be extended. But the code tries to extend it withSportsCar.Step 2: Identify the error
SinceCaris final, extending it causes a fatal error. But the question is about overriding a final method. Actually, the class is final, so extending is not allowed at all.Step 3: Check options carefully
Fatal error: Cannot extend final class Car says cannot extend final class, which matches the error. Fatal error: Cannot override final method Car::start() says cannot override final method, which would be error if class was not final but method was final.Final Answer:
Fatal error: Cannot extend final class Car -> Option DQuick Check:
final class cannot be extended = fatal error [OK]
- Confusing final method error with final class error
- Assuming method override works despite final class
- Ignoring that class is final and cannot be extended
class Vehicle {
final public function move() {
return "Moving";
}
}
class Bike extends Vehicle {
public function move() {
return "Bike moving";
}
}Solution
Step 1: Understand final method behavior
The methodmove()inVehicleis declared asfinal, so child classes cannot override it.Step 2: Check the child class method
TheBikeclass tries to overridemove(), which is not allowed and causes a fatal error.Final Answer:
Cannot override final method move() in class Bike -> Option CQuick Check:
final method cannot be overridden [OK]
- Thinking final method can be overridden
- Confusing final method with abstract method
- Assuming no error when overriding final method
Document that cannot be extended, but you want to allow extending a method print() in child classes. Which is the correct approach?Solution
Step 1: Understand final class effect
Declaring a class asfinalprevents any extension, so no child classes can exist to override methods.Step 2: Understand method overriding
To allow overridingprint(), the class must be extendable and the method must not be final.Step 3: Choose correct option
Do not declare class Document as final, and declare method print() normally allows extending the class and overriding the method. Options B and D prevent extension. Do not declare class Document as final, but declare method print() as final prevents method overriding.Final Answer:
Do not declare class Document as final, and declare method print() normally -> Option AQuick Check:
Allow extension and method override = no final on class or method [OK]
- Declaring class final but expecting method override
- Declaring method final when override is needed
- Confusing final class and final method effects
Ready to prove it?
Pass the test and this pattern moves unknown
