Php destructor _ destruct when to execute

Source: Internet
Author: User
Php destructor _ destruct: Under what conditions is the execution of all conditions met? & Amp; gt; php Terminator also executes php destructor _ destruct under what circumstances
Find all the situations, right?> Php Terminator is also executed

Reply content:

Php destructor _ destruct when to execute
Find all the situations, right?> Php Terminator is also executed

RTFM

Http://php.net/manual/en/language.oop5.decon.php#object.destruct

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

Run the command before the object is destroyed?

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 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;

The running result is as follows:

unset $c2unset $c1sf::__destructnull $c2null $c1sf::__destruct123 $c2456 $c1sf::__destructthe end

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

Generally, at the end of the script (rather than unset), php will destroy the reference and run it before the script stops running.

Exit (); also executed. It hurts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.