class __destruct() and register_shutdown_function()
Since PHP 5.0.5 the order of destruction and calling shutdown functions has been changed.
If you think you can call object instances thru these then they will fail (without notice)
The issues:
1. object instances get destroyed BEFORE the shutdown functions are called.
2. object instances are destroyed in order of creation NOT in reverse order.
To avoid these issues you must create your own "destruction collector" and register all object instances that need access to other objects.
We can achieve this by writing a class that has an __destruct() and create an instance of the class right at the beginning of execution.
PHP Code:
<?php
class my_proper_destructor
{
protected $destroyers = array();
public function add($class_instance)
{
if (is_class($class_instance))
$this->destroyers[] = $class_instance;
}
public function __destruct()
{
foreach($this->destroyers as $des)
$des->on_destroy();
}
}
$destruct = new my_proper_destructor();
// Your code goes here
Now when you write a class that needs other objects on destruction you slightly modify this class to use
PHP Code:
class session
{
function __construct()
{
global $destruct;
$destruct->add($this);
}
function on_destroy()
{
// destruction code goes here instead of using __destruct()
}
}