Destructor method in PHP - Time & Space Complexity
We want to understand how the time it takes to run a destructor method changes as the program runs.
Specifically, how does the destructor's work grow when objects are destroyed?
Analyze the time complexity of the following code snippet.
class Example {
public function __destruct() {
echo "Cleaning up...\n";
}
}
$objects = [];
for ($i = 0; $i < 5; $i++) {
$objects[] = new Example();
}
// Objects are destroyed automatically at script end
This code creates 5 objects, each with a destructor that runs when the object is destroyed.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The destructor method runs once for each object destroyed.
- How many times: It runs exactly as many times as there are objects created (5 times here).
Each new object adds one destructor call when it is destroyed.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 destructor calls |
| 100 | 100 destructor calls |
| 1000 | 1000 destructor calls |
Pattern observation: The number of destructor calls grows directly with the number of objects.
Time Complexity: O(n)
This means the time to run all destructors grows in a straight line as the number of objects increases.
[X] Wrong: "The destructor runs only once no matter how many objects exist."
[OK] Correct: Each object has its own destructor call, so more objects mean more destructor executions.
Understanding how destructors work helps you reason about cleanup tasks and resource management in real programs.
"What if the destructor method contained a loop that processed an array of size m? How would the time complexity change?"
Practice
__destruct method in a PHP class?Solution
Step 1: Understand the role of
The__destruct__destructmethod is called automatically when an object is destroyed or goes out of scope.Step 2: Identify its purpose
It is used to clean up resources like closing files or database connections safely.Final Answer:
To clean up resources when an object is destroyed -> Option AQuick Check:
Destructor method = cleanup on object destruction [OK]
- Confusing __destruct with __construct
- Thinking it initializes objects
- Using it to handle input validation
Solution
Step 1: Recall PHP destructor naming
PHP destructor methods must be named exactly__destructwith two underscores.Step 2: Check syntax correctness
The correct syntax isfunction __destruct() {}. Other options have incorrect names.Final Answer:
function __destruct() {} -> Option AQuick Check:
Destructor syntax = __destruct() [OK]
- Using single underscore or wrong method name
- Omitting parentheses in method declaration
- Confusing with __construct method
class Test {
function __construct() {
echo "Constructed\n";
}
function __destruct() {
echo "Destructed\n";
}
}
$obj = new Test();
echo "Hello\n";
unset($obj);Solution
Step 1: Trace object creation and constructor call
When$obj = new Test();runs,__construct()prints "Constructed\n".Step 2: Print "Hello" and then unset object
Next, "Hello\n" is printed. Thenunset($obj);destroys the object, triggering__destruct()which prints "Destructed\n".Final Answer:
Constructed\nHello\nDestructed\n -> Option DQuick Check:
Constructor then echo then destructor [OK]
- Assuming destructor runs before echo
- Confusing order of constructor and destructor
- Missing that unset triggers destructor immediately
class Sample {
function __destruct {
echo "Cleaning up";
}
}Solution
Step 1: Check destructor method syntax
The destructor method must include parentheses even if it has no parameters:function __destruct() {}.Step 2: Identify the missing parentheses error
The code misses parentheses after__destruct, causing a syntax error.Final Answer:
Missing parentheses in destructor declaration -> Option CQuick Check:
Destructor needs () after name [OK]
- Omitting parentheses in method declaration
- Using wrong destructor name
- Thinking destructor must be static
class FileHandler {
private $file;
function __construct($filename) {
$this->file = fopen($filename, 'w');
}
// What goes here?
}Solution
Step 1: Understand resource cleanup in destructor
The destructor should close the file resource held in$this->fileusingfclose().Step 2: Check each option for correctness
function __destruct() { fclose($this->file); } correctly callsfclose($this->file);. function __destruct() { open($this->file); } tries to open instead of close. function __destruct() { fclose($filename); } uses undefined$filename. function __destruct() { $this->file = null; } just sets property to null without closing.Final Answer:
function __destruct() { fclose($this->file); } -> Option BQuick Check:
Destructor closes file resource properly [OK]
- Calling wrong function like open() instead of fclose()
- Using undefined variables inside destructor
- Not actually closing the resource, just nullifying
Ready to prove it?
Pass the test and this pattern moves unknown
