0
0
PHPprogramming~20 mins

Destructor method in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Destructor Mastery
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 destructor example?

Consider the following PHP code. What will it output when run?

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

Remember that the destructor runs when the object is destroyed, usually at the end of the script.

Predict Output
intermediate
2:00remaining
What happens when an object is unset explicitly?

What will this PHP code output?

PHP
<?php
class Sample {
    public function __destruct() {
        echo "Bye!\n";
    }
}
$obj = new Sample();
unset($obj);
echo "After unset\n";
?>
A
Bye!
B
After unset
Bye!
C
Bye!
After unset
D
After unset
Attempts:
2 left
💡 Hint

Think about when the destructor is called if you unset the object manually.

🔧 Debug
advanced
2:00remaining
Why does this destructor not run as expected?

Look at this PHP code. The destructor message is never printed. Why?

PHP
<?php
class Demo {
    public function __destruct() {
        echo "Destroying\n";
    }
}
function create() {
    $obj = new Demo();
}
create();
echo "Done\n";
?>
AThe destructor is not called because PHP requires explicit unset to run destructor.
BThe destructor is never called because the object is inside a function and goes out of scope silently.
CThe destructor is called but output is buffered and not shown.
DThe object is destroyed immediately after create() ends, so destructor runs before echo "Done".
Attempts:
2 left
💡 Hint

Think about when local variables inside functions are destroyed.

📝 Syntax
advanced
2:00remaining
Which option shows the correct destructor syntax in PHP?

Choose the correct way to declare a destructor method in a PHP class.

Apublic function __destruct() {}
Bpublic function destruct() {}
Cfunction __destruct {}
Dpublic function ~destruct() {}
Attempts:
2 left
💡 Hint

Destructor method name must be exactly __destruct with parentheses.

🚀 Application
expert
3:00remaining
What is the output when multiple objects with destructors are created and unset?

Consider this PHP code. What will be the exact output?

PHP
<?php
class A {
    public function __destruct() {
        echo "A destroyed\n";
    }
}
class B {
    public function __destruct() {
        echo "B destroyed\n";
    }
}
$a = new A();
$b = new B();
unset($a);
echo "Middle\n";
?>
A
A destroyed
Middle
B destroyed
B
Middle
A destroyed
B destroyed
C
A destroyed
B destroyed
Middle
D
B destroyed
A destroyed
Middle
Attempts:
2 left
💡 Hint

Think about when each object is destroyed and when the script ends.