note 40817 added to function.register-shutdown-function

From: Date: Mon, 15 Mar 2004 21:17:54 +0000
Subject: note 40817 added to function.register-shutdown-function
Groups: php.notes 
Request: Send a blank email to php-notes+get-66882@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. <? $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? ---- Manual Page -- http://www.php.net/manual/en/function.register-shutdown-function.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40817 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40817&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40817&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#66882) next »