PHP: Under what circumstances is the destructor method __destruct executed?

黄舟
Release: 2023-03-12 11:02:02
Original
4006 people have browsed it

php Destructor method Under what circumstances is destruct executed? In all cases, is it encountered?>php terminator is also executed

Destructor function Will be executed when all references to a certain object are deleted or when the object is explicitly destroyed

Executed before the object is destroyed Bar?

According to the official manual, the destructor will be executed when all references to an object are deleted or when the object is explicitly destroyed. The code demonstration is as follows

class sf{    public function destruct()
    {        echo METHOD . PHP_EOL;
    }
}


$c1 = new sf;
$c2 = $c1;echo 'unset $c2' . PHP_EOL;unset($c2);echo 'unset $c1' . PHP_EOL;unset($c1);// ----$c1 = new sf;
$c2 = $c1;echo 'null $c2' . PHP_EOL;
$c2 = null;echo 'null $c1' . PHP_EOL;
$c1 = null;// ----$c1 = new sf;
$c2 = $c1;echo '123 $c2' . PHP_EOL;
$c2 = 123;echo '456 $c1' . PHP_EOL;
$c1 = 456;echo 'the end' . PHP_EOL;
Copy after login

The running results are as follows

unset $c2unset $c1
sf::destructnull $c2null $c1
sf::destruct123 $c2456 $c1
sf::destruct
the end
Copy after login

The destructor will be executed when all references to an object are deleted or when the object is explicitly destroyed.

Generally speaking, php will destroy the reference when the script ends (not unset) and runs before the script ends.

The above is the detailed content of PHP: Under what circumstances is the destructor method __destruct executed?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template