note 40817 deleted from function.register-shutdown-function by crell

From: Date: Wed, 30 Mar 2022 22:07:21 +0000
Subject: note 40817 deleted from function.register-shutdown-function by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-217812@lists.php.net to get a copy of this message
Note Submitter: Jim Smith ---- 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?

« previous php.notes (#217812) next »