PHP5 calls __destruct () if you use redirection?

I found that PHP5 does not call the __destruct () function if I have the following setting:

class test {
 __destruct ()
 {
  echo 'hehe';
  exit
 }
}

header ('Location: http://test.com/');
exit

It never calls the destruct function

+3
source share
1 answer

destructor is called:

  • for any object that you created
    • in the part of the script that you posted, you did not initiate any object - perhaps the reason the destructor was not called?
  • php script at the end

Using a header for redirection does not prevent the destructor from being called.


, PHP script - , , "", .

, :

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;

( , instanciation )

"hehe" , , /tmp/test-desctructor.txt:

$ cat /tmp/test-desctructor.txt
glop

, "hehe" .


, , , .

, manual ( - ):

. , script HTTP- .

"hehe" : ; ; -)

, btw;-)

+12

Source: https://habr.com/ru/post/1718235/


All Articles