Home > Backend Development > PHP Tutorial > Two solutions for PHP destructor method __destruct() not triggering

Two solutions for PHP destructor method __destruct() not triggering

藏色散人
Release: 2023-04-06 10:12:02
forward
3576 people have browsed it

This article mainly introduces two solutions to the problem that the PHP destructor method __destruct() is not triggered.

Sometimes when a class is referenced cyclically in PHP, it will cause the problem that __destruct() is not triggered. First, let’s look at the problem code:

<?php
class Proxy
{
    private $object;
 
    public function __construct($object)
    {
        $this->object = $object;
    }
 
    public function __destruct()
    {
        var_dump(&#39;__destruct:Proxy&#39;);
    }
}
 
class Test
{
    private $proxy;
 
    public function __construct()
    {
        $this->proxy = new Proxy($this);
    }
 
    public function __destruct()
    {
        var_dump(&#39;__destruct:Test&#39;);
    }
}
 
$test = new Test;
unset($test);
 
echo &#39;no __destruct, wait 3s&#39;, PHP_EOL;
 
sleep(3);
 
echo &#39;__destruct now:&#39;, PHP_EOL;
Copy after login

As shown in the above code, when running unset($test), __destruct() will not be triggered because there is a circular reference.

Look at the code of solution 1 below:

<?php
class Proxy
{
    private $object;
 
    public function __construct($object)
    {
        $this->object = $object;
    }
 
    public function __destruct()
    {
        var_dump(&#39;__destruct:Proxy&#39;);
    }
}
 
class Test
{
    private $proxy;
 
    public function __construct()
    {
        $this->proxy = new Proxy($this);
    }
 
    public function __destruct()
    {
        var_dump(&#39;__destruct:Test&#39;);
    }
 
    public function close()
    {
        $this->proxy = null;
    }
}
 
$test = new Test;
$test->close();
 
echo &#39;__destruct now:&#39;, PHP_EOL;
 
unset($test);
 
sleep(3);
 
echo &#39;no operation&#39;, PHP_EOL;
Copy after login

In the above code, before unset, set the proxy in the Test class to null, and then unset again to trigger __destruct ().

Of course, you can also manually gc (solution 2):

<?php
class Proxy
{
    private $object;
 
    public function __construct($object)
    {
        $this->object = $object;
    }
 
    public function __destruct()
    {
        var_dump(&#39;__destruct:Proxy&#39;);
    }
}
 
class Test
{
    private $proxy;
 
    public function __construct()
    {
        $this->proxy = new Proxy($this);
    }
 
    public function __destruct()
    {
        var_dump(&#39;__destruct:Test&#39;);
    }
}
 
$test = new Test;
unset($test);
 
echo &#39;__destruct now:&#39;, PHP_EOL;
gc_collect_cycles();
 
sleep(3);
 
echo &#39;no operation&#39;, PHP_EOL;
Copy after login

I hope it will be helpful to friends in need!

The above is the detailed content of Two solutions for PHP destructor method __destruct() not triggering. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template