PHP callback before script dies

I have a set of custom PHP scripts. They load the main PHP file (to include other files, set options, etc.). From this script, I would like to insert a callback function called by scripts that included the main file before ending.

What I intend to do is make a simple custom output cache using ob_get_contents()

. I want to do ob_start()

in main.php, after which the callback function will save the output.

+2


source to share


2 answers


ob_start()

can do what you want:

ob_start('store_output');

function store_output($output) {
  // do something with $output
  return $output;
}

      



When the output buffer is flushed, the function will be called. Output buffers can be nested.

+1


source


See register_shutdown_function

:

Register a function to execute shutdown



Example from the manual:

<?php
function shutdown()
{
    // This is our shutdown function, in 
    // here we can do any last operations
    // before the script is complete.

    echo 'Script executed with success', PHP_EOL;
}

register_shutdown_function('shutdown');
?>

      

+4


source







All Articles