本篇文章主要给大家介绍PHP 析构方法 __destruct() 不触发的两个解决办法。
有时候在 PHP 里类循环引用时,会导致 __destruct() 不触发的问题,先上问题代码:
<?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;
希望对需要的朋友有所帮助!
Atas ialah kandungan terperinci PHP析构方法 __destruct() 不触发的两个解决办法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!