I was trying to figure out how to pass parameters to the register_shutdown_function() since you cannot register a function with parameters and passing through globals is not appropriate. E.g. what I was trying to do was <? register_shutdown_function("anotherfunction('parameter')") ?>
Turns out, the trick is to use create_function() to create a "function" that calls the desired function with static parameters.
<?php
$funtext="mail('u@ho.com','mail test','sent after shutdown');";
register_shutdown_function(create_function('',$funtext));
?>
Here's another example showing in-line logging and a post-execution version:
Before: in-process logging
<?php
function logit($message) {
$oF=fopen('TEST.log', 'a');
fwrite($oF,"$message\n");
fclose($oF);
sleep(5); }
print "loging";
logit("traditional execution");
print "logged";
exit;
?>
After:
<?php
function logit($message) {
$forlater=create_function('',"loglater('$message');");
register_shutdown_function($forlater);
}
function loglater($message) {
$oF=fopen('TEST.log', 'a');
fwrite($oF,"$message\n");
fclose($oF);
sleep(5); }
print "loging";
logit("delayed execution");
print "logged";
exit;
?>
In the 'before' example, the file is written (and the delay occurs) before the "logged" appears. In the 'after' example, the file is written after execution terminates.
Maybe it would be nice to add a parameter to the register_shutdown_function that does this automatically?