Давай проверим с циклическими ссылками.
class A
{
public $b;
public function __construct()
{
$this->b = new B($this);
}
public function __destruct()
{
echo "Destructor A\n";
}
}
class B
{
public $a;
public function __construct($a)
{
$this->a = $a;
}
public function __destruct()
{
echo "Destructor B\n";
}
}
$t = 1;
$a = new A();
echo "Test point ", $t++, "\n";
unset($a);
echo "Test point ", $t++, "\n";
$ php t2.php
Test point 1
Test point 2
Destructor A
Destructor B
Циклические ссылки не позволяют уничтожить объект сразу...
Сделаем принудительно:
class A
{
public $b;
public function __construct()
{
$this->b = new B($this);
}
public function __destruct()
{
echo "Destructor A\n";
}
public function cleanup()
{
unset($this->b);
}
}
class B
{
public $a;
public function __construct($a)
{
$this->a = $a;
}
public function __destruct()
{
echo "Destructor B\n";
}
}
$t = 1;
$a = new A();
echo "Test point ", $t++, "\n";
$a->cleanup();
echo "Test point ", $t++, "\n";
unset($a);
echo "Test point ", $t++, "\n";
$ php t3.php
Test point 1
Destructor B
Test point 2
Destructor A
Test point 3
Очистка гарантирована.