note 40817 modified in function.register-shutdown-function by danbrown
| From: | danbrown@php.net | Date: | Sat, 19 Jul 2008 23:10:51 +0000 |
| Subject: | note 40817 modified in function.register-shutdown-function by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-141785@lists.php.net to get a copy of this message | ||
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); // so you can see the delay
}
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); // so you can see the delay
}
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?
--was--
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.
<?
$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
<?
function logit($message) {
$oF=fopen('TEST.log', 'a');
fwrite($oF,"$message\n");
fclose($oF);
sleep(5); // so you can see the delay
}
print "loging";
logit("traditional execution");
print "logged";
exit;
?>
After:
<?
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); // so you can see the delay
}
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?
http://php.net/manual/en/function.register-shutdown-function.php