Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <a href="http://php.net/manual/en/language.oop5.decon.php#language.oop5.decon.destructor" rel="noreferrer">destructor</a> is called :</p> <ul> <li>for whatever object you have instanciated <ul> <li>in the portion of script you posted, you have not instanciated any object -- maybe that's the cause of no destructor being called ?</li> </ul></li> <li>at the end of the PHP script</li> </ul> <p>Using a header to redirect doesn't prevent the destructor from being called.</p> <p><br> Also note that the destructor is called at the end of the PHP script -- but doesn't prevent the redirection, as the header saying "redirect" has already been generated.</p> <p>For instance, with this code :</p> <pre><code>class Test { public function __destruct() { echo 'hehe'; file_put_contents('/tmp/test-desctructor.txt', "glop\n"); exit; } } $a = new Test(); header('Location: http://example.com/'); exit; </code></pre> <p><em>(Note that I corrected a few mistakes, and added an actual instanciation of the class)</em></p> <p>You will not see "<code>hehe</code>" on the output, but you'll find that the file <code>/tmp/test-desctructor.txt</code> has been created :</p> <pre><code>$ cat /tmp/test-desctructor.txt glop </code></pre> <p>You'll need to remove the redirection if you want to get the "<code>hehe</code>" on the ouput.</p> <p><br> The destructor is called after the header has been generated -- and calling exit from the destructor will not change the fact that that header has already been generated.</p> <p>Oh, and here is a note from the <a href="http://php.net/manual/en/language.oop5.decon.php#language.oop5.decon.destructor" rel="noreferrer">manual</a> <em>(quoting -- at the bottom of the page)</em> :</p> <blockquote> <p>Note: Destructors called during the script shutdown have HTTP headers already sent.</p> </blockquote> <p>This is why you don't see your "<code>hehe</code>" string : the destructor is called ; you just don't see it on the screen ;-)</p> <p>That's why I used a file in my example, btw ;-) </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload