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

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

发表评论

电子邮件地址不会被公开。 必填项已用*标注