Constructor inheritance in PHP - Time & Space Complexity
Let's see how the time it takes to run code changes when using constructor inheritance in PHP classes.
We want to know how the program's work grows as we create more objects.
Analyze the time complexity of the following code snippet.
class ParentClass {
public function __construct() {
// Some setup work
for ($i = 0; $i < 100; $i++) {
// Simulate work
}
}
}
class ChildClass extends ParentClass {
public function __construct() {
parent::__construct();
// Additional setup
}
}
$objects = [];
for ($j = 0; $j < $n; $j++) {
$objects[] = new ChildClass();
}
This code creates many objects of a child class that calls the parent constructor each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating
$nobjects, each running a loop of 100 steps in the parent constructor. - How many times: The outer loop runs
$ntimes, and inside each, the parent constructor runs a fixed 100-step loop.
Each new object adds the same fixed amount of work from the parent constructor.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 x 100 = 1,000 |
| 100 | 100 x 100 = 10,000 |
| 1000 | 1000 x 100 = 100,000 |
Pattern observation: The total work grows directly with the number of objects created.
Time Complexity: O(n)
This means the time to run grows in a straight line as you create more objects.
[X] Wrong: "Calling the parent constructor inside the child constructor makes the time grow faster than the number of objects."
[OK] Correct: The parent constructor work is fixed and does not depend on n, so it just adds a constant amount of work per object.
Understanding how constructors run when inherited helps you explain object creation costs clearly in interviews.
"What if the parent constructor had a loop that depends on n instead of a fixed 100? How would the time complexity change?"
Practice
Solution
Step 1: Understand default constructor behavior
If the child class does not define a constructor, PHP automatically calls the parent's constructor when creating a child object.Step 2: Confirm no error or override
Since the child has no constructor, no error occurs and the parent's constructor runs as expected.Final Answer:
The parent's constructor is automatically called when creating a child object. -> Option CQuick Check:
Child without constructor calls parent constructor [OK]
- Thinking child constructor must exist to call parent constructor
- Assuming PHP throws error if child lacks constructor
- Believing child inherits parent's constructor as a method
Solution
Step 1: Recall PHP parent constructor call syntax
In PHP, the parent's constructor is called usingparent::__construct();inside the child's constructor.Step 2: Eliminate incorrect syntax
Options A, C, and D use invalid syntax: 'constructor' is not the method name, 'super' is not PHP keyword, and 'parent->' is incorrect.Final Answer:
parent::__construct(); -> Option AQuick Check:
Use parent::__construct() to call parent constructor [OK]
- Using 'super' keyword instead of 'parent'
- Calling parent constructor as parent::constructor()
- Using object operator '->' with parent keyword
class ParentClass {
public function __construct() {
echo "Parent constructor called. ";
}
}
class ChildClass extends ParentClass {
public function __construct() {
parent::__construct();
echo "Child constructor called.";
}
}
new ChildClass();Solution
Step 1: Trace constructor calls
The child class constructor callsparent::__construct();first, so the parent's constructor runs and prints "Parent constructor called. "Step 2: Continue child constructor execution
After the parent's constructor, the child constructor prints "Child constructor called." immediately after.Final Answer:
Parent constructor called. Child constructor called. -> Option BQuick Check:
Child calls parent constructor then prints own message [OK]
- Ignoring parent::__construct() call in child constructor
- Assuming only child constructor output appears
- Thinking parent's constructor is not called explicitly
class Base {
public function __construct() {
echo "Base constructor.";
}
}
class Derived extends Base {
public function __construct() {
parent::construct();
echo "Derived constructor.";
}
}
new Derived();Solution
Step 1: Check parent constructor call syntax
The code callsparent::construct();but the correct method name is__construct()with double underscores.Step 2: Understand impact of wrong method name
Because 'construct()' does not exist, PHP will throw a fatal error or warning about undefined method.Final Answer:
The parent constructor is called with wrong method name 'construct()'. -> Option AQuick Check:
Use parent::__construct(), not parent::construct() [OK]
- Calling parent::construct() instead of parent::__construct()
- Forgetting double underscores in constructor name
- Assuming parent constructor runs without correct call
Child object?class Parent {
public function __construct() {
echo "Parent start. ";
}
}
class Child extends Parent {
public function __construct() {
echo "Child start. ";
parent::__construct();
echo "Child end.";
}
}
new Child();Solution
Step 1: Follow the order of echo statements
The child constructor first prints "Child start. ". Then it callsparent::__construct();which prints "Parent start. ". Finally, the child prints "Child end.".Step 2: Combine outputs in sequence
Putting all together, the output is "Child start. Parent start. Child end." exactly in that order.Final Answer:
Child start. Parent start. Child end. -> Option DQuick Check:
Child prints before and after parent constructor call [OK]
- Assuming parent constructor runs before child's code
- Ignoring order of echo statements
- Thinking parent's constructor output appears last
Ready to prove it?
Pass the test and this pattern moves unknown
