I found that PHP5 isn't calling a __destruct() function if I have the following setup:
class test { __destruct() { echo 'hehe'; exit; } } header('Location: http://test.com/'); exit;
It never calls the destruct function
I found that PHP5 isn't calling a __destruct() function if I have the following setup:
class test { __destruct() { echo 'hehe'; exit; } } header('Location: http://test.com/'); exit;
It never calls the destruct function
The destructor is called :
Using a header to redirect doesn't prevent the destructor from being called.
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.
For instance, with this 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;
(Note that I corrected a few mistakes, and added an actual instanciation of the class)
You will not see "hehe
" on the output, but you'll find that the file /tmp/test-desctructor.txt
has been created :
$ cat /tmp/test-desctructor.txt
glop
You'll need to remove the redirection if you want to get the "hehe
" on the ouput.
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.
Oh, and here is a note from the manual (quoting -- at the bottom of the page) :
Note: Destructors called during the script shutdown have HTTP headers already sent.
This is why you don't see your "hehe
" string : the destructor is called ; you just don't see it on the screen ;-)
That's why I used a file in my example, btw ;-)