PHP shutdown sequence

I needed to figure out in what order PHP shuts down, after the end of a script has been reached, so I created a small testing script. Maybe this is of use for someone else trying to google this like I tried.

  1. <?php
  2.  
  3. // Testing shutdown sequence
  4.  
  5. function shutdown() {
  6.  
  7. echo "register_shutdown_function\n";
  8.  
  9. }
  10.  
  11. register_shutdown_function('shutdown');
  12.  
  13.  
  14. class MyClass {
  15.  
  16. function __destruct() {
  17.  
  18. echo "Object destructor\n";
  19.  
  20. }
  21.  
  22.  
  23. }
  24.  
  25. function obcallback($buffer) {
  26.  
  27. $buffer .= "Output buffer callback\n";
  28. return $buffer;
  29.  
  30. }
  31.  
  32. ob_start('obcallback');
  33.  
  34. $myObject = new MyClass;
  35.  
  36. function dummy() { }
  37.  
  38. function sessionclose() {
  39.  
  40. echo "Session close\n";
  41.  
  42. }
  43.  
  44. function sessionwrite() {
  45.  
  46. echo "Session write\n";
  47. }
  48.  
  49. session_set_save_handler('dummy','sessionclose','dummy','sessionwrite','dummy','dummy');
  50.  
  51. session_start();
  52.  
  53. ?>

The output, on PHP 5.2.0 on the cli is:

  1. register_shutdown_function
  2. Object destructor
  3. Output buffer callback
  4. Session write
  5. Session close

I was mostly interested in this because I wanted to work with a custom session handler. This means I can basically not use objects in combination with session handlers, unless I don't rely on $this.

 1

About

My name is Evert, and I've been writing semi-regularly on this blog since 2006.

I'm currently available for contract work.

more info.

Subscribe

Dropbox

Dropbox is a simple cross-platform online backup and sync application. The first 2GB of space is free, and both you and me get an extra 250MB extra space if you sign up through this link.