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;
赞(0)
声明:本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-62778877-8306;邮箱:fanjiao@west.cn。本站原创内容未经允许不得转载,或转载时需注明出处:西部数码知识库 » PHP析构方法 __destruct() 不触发的两个解决办法

登录

找回密码

注册