本篇文章主要介紹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;
希望對需要的朋友有幫助!
以上是PHP析構方法 __destruct() 不觸發的兩個解決方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!