0
0
PHPprogramming~20 mins

__construct and __destruct in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Constructor & Destructor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code with __construct and __destruct?

Consider the following PHP class with constructor and destructor methods. What will be printed when the script runs?

PHP
<?php
class Test {
    public function __construct() {
        echo "Start\n";
    }
    public function __destruct() {
        echo "End\n";
    }
}
$obj = new Test();
echo "Middle\n";
?>
A
Start
Middle
End
B
Start
End
Middle
C
Middle
Start
End
D
End
Start
Middle
Attempts:
2 left
💡 Hint

Remember that the constructor runs when the object is created, and the destructor runs when the script ends or the object is destroyed.

Predict Output
intermediate
2:00remaining
What happens if __destruct modifies a property before script ends?

Look at this PHP code. What will be the output?

PHP
<?php
class Counter {
    public $count = 0;
    public function __construct() {
        $this->count = 1;
        echo "Construct: {$this->count}\n";
    }
    public function __destruct() {
        $this->count++;
        echo "Destruct: {$this->count}\n";
    }
}
$obj = new Counter();
echo "Count: {$obj->count}\n";
?>
A
Construct: 1
Count: 1
Destruct: 2
B
Construct: 1
Destruct: 2
Count: 1
C
Count: 0
Construct: 1
Destruct: 2
D
Construct: 1
Count: 2
Destruct: 3
Attempts:
2 left
💡 Hint

The destructor runs after the last output, so the property change is visible only inside the destructor.

🔧 Debug
advanced
2:00remaining
What error does this PHP code cause?

Examine this PHP class. What error will occur when running this code?

PHP
<?php
class Sample {
    public function __construct() {
        echo "Hello\n";
    }
}
$obj = new Sample();
?>
ANo error, outputs: Hello
BFatal error: Uncaught Error: Call to undefined method
CParse error: syntax error, unexpected '}'
DParse error: syntax error, unexpected end of file
Attempts:
2 left
💡 Hint

Check the syntax carefully, especially punctuation after statements.

🧠 Conceptual
advanced
1:30remaining
When is __destruct called in PHP?

Choose the correct statement about when the __destruct method is called in PHP.

A__destruct is called immediately after the constructor finishes.
B__destruct is called when the object is no longer referenced or script ends.
C__destruct is called before the constructor runs.
D__destruct is called only when the object is explicitly destroyed using unset().
Attempts:
2 left
💡 Hint

Think about object lifecycle and script execution.

🚀 Application
expert
2:30remaining
How many times is __destruct called in this PHP script?

Analyze this PHP code. How many times will the __destruct method be called before the script ends?

PHP
<?php
class Logger {
    public function __construct() {
        echo "Logger started\n";
    }
    public function __destruct() {
        echo "Logger ended\n";
    }
}
function createLogger() {
    $log = new Logger();
}
createLogger();
$log = new Logger();
?>
A3
B1
C0
D2
Attempts:
2 left
💡 Hint

Consider when objects go out of scope and when destructors run.