I don't understand why
class test {
function __destruct() {
echo "I am the destructor<br>";
}
function __construct() {
$this->a=debug_backtrace(); # <=== THIS LINE
}
}
$myclass=new test();
unset($myclass);
echo "Last Line<br>";
out- and incommenting this line changes the order of the echo output. Why is the destructor not called by the unset() when the contsructor did a debug_backtrace() before?
By default, the backtrace returned by
debug_backtrace()includes the function arguments including the implicit$thisargument asobject. Therefore, there's a cycle where thetestinstance referenced by$myclassalso indirectly references itself, thereby keeping itself alive.If you use
... = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)that cycle is no longer present and the destructor is called right after theunset($myclass)call.You can also call
gc_collect_cycles()afterunset, which will run the cycle collector and free thetestinstance, calling the destructor in the process.