<?php class Proxy { private $object; public function __construct($object) { $this->object = $object; } public function __destruct() { var_dump('__destruct:Proxy'); } } class Test { private $proxy; public function __construct() { $this->proxy = new Proxy($this); } public function __destruct() { var_dump('__destruct:Test'); } } $test = new Test; unset($test); echo 'no __destruct, wait 3s', PHP_EOL; sleep(3); echo '__destruct now:', PHP_EOL;
如上代码,运行unset($test)时,不会触发__destruct(),因为有了循环引用。
再看下面的解决方法1的代码:
<?php class Proxy { private $object; public function __construct($object) { $this->object = $object; } public function __destruct() { var_dump('__destruct:Proxy'); } } class Test { private $proxy; public function __construct() { $this->proxy = new Proxy($this); } public function __destruct() { var_dump('__destruct:Test'); } public function close() { $this->proxy = null; } } $test = new Test; $test->close(); echo '__destruct now:', PHP_EOL; unset($test); sleep(3); echo 'no operation', PHP_EOL;
上面的代码,在unset之前,将Test类中的proxy设为null,然后再unset,就可以触发__destruct()了。
当然,你也可以手动gc(解决方法2):
<?php class Proxy { private $object; public function __construct($object) { $this->object = $object; } public function __destruct() { var_dump('__destruct:Proxy'); } } class Test { private $proxy; public function __construct() { $this->proxy = new Proxy($this); } public function __destruct() { var_dump('__destruct:Test'); } } $test = new Test; unset($test); echo '__destruct now:', PHP_EOL; gc_collect_cycles(); sleep(3); echo 'no operation', PHP_EOL;