Type hinting with parent classes in PHP - Time & Space Complexity
We want to understand how the time it takes to run code changes when using type hints with parent classes in PHP.
Specifically, how does checking types against parent classes affect the speed as input grows?
Analyze the time complexity of the following code snippet.
class Animal {}
class Dog extends Animal {}
function feedAnimal(Animal $animal) {
// feeding logic here
}
$dogs = array_fill(0, $n, new Dog());
foreach ($dogs as $dog) {
feedAnimal($dog);
}
This code creates an array of Dog objects and feeds each one using a function that requires an Animal type hint.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array of Dog objects.
- How many times: Exactly once for each Dog in the array, so n times.
As the number of Dog objects increases, the feeding function is called once per object.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to feedAnimal() |
| 100 | 100 calls to feedAnimal() |
| 1000 | 1000 calls to feedAnimal() |
Pattern observation: The number of operations grows directly with the number of objects.
Time Complexity: O(n)
This means the time to run the code grows in a straight line as the number of objects increases.
[X] Wrong: "Type hinting with parent classes slows down the code exponentially because it checks all subclasses."
[OK] Correct: PHP only checks the type once per call, and it does not repeatedly check all subclasses. The time grows linearly with the number of calls, not exponentially.
Understanding how type hinting affects performance helps you write clear and efficient code, a skill valued in real projects and interviews.
"What if we changed the array to hold different subclasses of Animal instead of just Dog? How would the time complexity change?"
Practice
What does type hinting with a parent class in PHP allow?
For example, using function foo(ParentClass $obj).
Solution
Step 1: Understand type hinting with parent classes
Type hinting with a parent class means the function accepts objects of that class or any subclass.Step 2: Apply to the example
UsingParentClass $objallows passing instances of ParentClass or any class that extends it.Final Answer:
It allows passing any object that is an instance of the parent class or its child classes. -> Option DQuick Check:
Parent class type hint = accepts parent or child objects [OK]
- Thinking only exact parent class objects are accepted
- Believing child classes are excluded
- Confusing type hinting with disabling checks
Which of the following is the correct syntax to type hint a function parameter with a parent class Animal in PHP?
function feed(____ $pet) { /* ... */ }Solution
Step 1: Recall PHP type hint syntax
In PHP, the type hint comes before the variable name:Type $variable.Step 2: Apply to the example
For class Animal, the correct syntax isAnimal $pet.Final Answer:
Animal $pet -> Option AQuick Check:
Type hint before variable name = Animal $pet [OK]
- Placing variable before type hint
- Adding extra keywords like 'class' or 'type'
- Omitting the $ sign on variable
Given the code below, what will be the output?
class Vehicle {}
class Car extends Vehicle {}
function startEngine(Vehicle $v) {
echo get_class($v);
}
$car = new Car();
startEngine($car);Solution
Step 1: Understand type hinting with inheritance
The function expects a Vehicle object or subclass. Car extends Vehicle, so passing Car is valid.Step 2: Analyze output of get_class()
get_class($v) returns the actual class name of the object, which is 'Car'.Final Answer:
Car -> Option CQuick Check:
Type hint accepts subclass; get_class returns actual class [OK]
- Assuming output is parent class name
- Expecting a type error for subclass
- Thinking no output is produced
What is the error in the following PHP code?
class Fruit {}
class Apple extends Fruit {}
function eat(Apple $fruit) {
echo "Eating apple";
}
$fruit = new Fruit();
eat($fruit);Solution
Step 1: Check type hint and argument
The function expects an Apple object, but the argument is a Fruit object (parent class).Step 2: Understand PHP type hint rules
PHP requires the argument to be the hinted class or subclass, not a parent class. Passing Fruit causes a fatal error.Final Answer:
Fatal error: Argument must be of type Apple, Fruit given -> Option BQuick Check:
Type hint mismatch with parent class argument = fatal error [OK]
- Thinking parent class can replace child hint
- Expecting no error on type mismatch
- Confusing syntax error with type error
Consider the following classes and function:
class Employee {}
class Manager extends Employee {}
class Developer extends Employee {}
function assignTask(Employee $e) {
if ($e instanceof Manager) {
return "Assigning management task";
} elseif ($e instanceof Developer) {
return "Assigning coding task";
} else {
return "Assigning general task";
}
}
$worker = new Manager();
echo assignTask($worker);What will be the output and why?
Solution
Step 1: Check type hint and argument
The function accepts any Employee or subclass. Manager is a subclass, so passing $worker is valid.Step 2: Evaluate instanceof conditions
The code checks if $e is a Manager, which is true, so it returns "Assigning management task".Final Answer:
Assigning management task -> Option AQuick Check:
Type hint accepts subclass; instanceof detects Manager [OK]
- Expecting type error for subclass argument
- Ignoring instanceof checks order
- Assuming general task is assigned
Ready to prove it?
Pass the test and this pattern moves unknown
