当前位置:Gxlcms > PHP教程 > PHP析构方法 __destruct() 不触发的两个解决办法

PHP析构方法 __destruct() 不触发的两个解决办法

时间:2021-07-01 10:21:17 帮助过:41人阅读

本篇文章主要给大家介绍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() 不触发的两个解决办法的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行