Constructor method in PHP - Time & Space Complexity
We want to understand how the time it takes to run a constructor method changes as the input grows.
Specifically, does creating an object take more time if we give it more data?
Analyze the time complexity of the following code snippet.
class Person {
public string $name;
public int $age;
public function __construct(string $name, int $age) {
$this->name = $name;
$this->age = $age;
}
}
$person = new Person("Alice", 30);
This code creates a Person object and sets its name and age when it is made.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Assigning values to object properties.
- How many times: Exactly twice here, once for each property.
As the number of properties to set grows, the time to run the constructor grows in a straight line.
| Input Size (number of properties) | Approx. Operations |
|---|---|
| 2 | 2 assignments |
| 10 | 10 assignments |
| 100 | 100 assignments |
Pattern observation: The time grows directly with the number of properties set.
Time Complexity: O(n)
This means the time to create the object grows in a straight line with the number of properties we assign.
[X] Wrong: "Constructors always run in constant time no matter what."
[OK] Correct: If the constructor sets many properties or processes large input, it takes longer. The time depends on what the constructor does.
Understanding how constructors scale helps you explain object creation costs clearly and shows you think about code efficiency in real projects.
"What if the constructor loops over an array of items to set properties? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a constructor does
A constructor is a special method that runs automatically when an object is created.Step 2: Identify the purpose of this automatic setup
This setup usually initializes properties or runs code needed for the object to work properly.Final Answer:
To automatically set up an object when it is created -> Option CQuick Check:
Constructor = automatic setup [OK]
- Thinking constructor deletes objects
- Confusing constructor with printing methods
- Assuming constructor creates new classes
Solution
Step 1: Recall the special constructor name in PHP
PHP uses the exact name __construct with two underscores at the start.Step 2: Check each option for correct syntax
Only function __construct() {} uses the correct spelling and underscores for the constructor.Final Answer:
function __construct() {} -> Option AQuick Check:
Constructor name = __construct [OK]
- Missing one or both underscores
- Using 'construct' without underscores
- Confusing constructor with destructor (__destruct)
class Car {
public $color;
public function __construct($color) {
$this->color = $color;
}
public function getColor() {
return $this->color;
}
}
$myCar = new Car('red');
echo $myCar->getColor();Solution
Step 1: Understand constructor parameter usage
The constructor sets the property color to the value passed ('red').Step 2: Check what getColor() returns
getColor() returns the color property, which was set to 'red'.Final Answer:
red -> Option AQuick Check:
Constructor sets color = 'red' [OK]
- Forgetting to pass argument to constructor
- Expecting class name as output
- Assuming property is null without initialization
class Person {
public $name;
public function __construct {
$this->name = 'Alice';
}
}Solution
Step 1: Check constructor syntax
In PHP, the constructor method must have parentheses even if no parameters are used.Step 2: Identify the missing parentheses
The code misses () after __construct, causing a syntax error.Final Answer:
Missing parentheses after __construct -> Option DQuick Check:
Constructor needs () even if empty [OK]
- Omitting parentheses in method declaration
- Thinking constructor name must match class
- Forgetting semicolon after statements
Book that stores a title and author. The constructor should accept both values and store them. Which code correctly implements this?Solution
Step 1: Check constructor name and parameters
PHP uses __construct with two parameters $title and $author, matching the requirement.Step 2: Verify property assignments
The code assigns both $title and $author to object properties using $this correctly.Final Answer:
class Book { public $title; public $author; public function __construct($title, $author) { $this->title = $title; $this->author = $author; } } -> Option BQuick Check:
Constructor with all needed parameters and assignments [OK]
- Using old-style constructor named after class
- Not passing all required parameters
- Not assigning parameters to properties
Ready to prove it?
Pass the test and this pattern moves unknown
