A lot of useful services may be delegated to this useful trigger.
It is very effective because it is executed at the end of the script but before any object destruction, so all instantiations are still alive.
Here's a simple shutdown events manager class which allows to manage either functions or static/dynamic methods, with an indefinite number of arguments without using any reflection, availing on a internal handling through func_get_args() and call_user_func_array() specific functions:
<?php
class shutdownScheduler {
private $callbacks; public function __construct() {
$this->callbacks = array();
register_shutdown_function(array($this, 'callRegisteredShutdown'));
}
public function registerShutdownEvent() {
$callback = func_get_args();
if (empty($callback)) {
trigger_error('No callback passed to '.__FUNCTION__.' method', E_USER_ERROR);
return false;
}
if (!is_callable($callback[0])) {
trigger_error('Invalid callback passed to the '.__FUNCTION__.' method', E_USER_ERROR);
return false;
}
$this->callbacks[] = $callback;
return true;
}
public function callRegisteredShutdown() {
foreach ($this->callbacks as $arguments) {
$callback = array_shift($arguments);
call_user_func_array($callback, $arguments);
}
}
public function dynamicTest() {
echo '_REQUEST array is '.count($_REQUEST).' elements long.<br />';
}
public static function staticTest() {
echo '_SERVER array is '.count($_SERVER).' elements long.<br />';
}
}
?>
A simple application:
<?php
function say($a = 'a generic greeting', $b = '') {
echo "Saying {$a} {$b}<br />";
}
$scheduler = new shutdownScheduler();
$scheduler->registerShutdownEvent('say', 'hello!');
$scheduler->registerShutdownEvent(array($scheduler, 'dynamicTest'));
$scheduler->registerShutdownEvent('scheduler::staticTest');
?>
It is easy to guess how to extend this example in a more complex context in which user defined functions and methods should be handled according to the priority depending on specific variables.
Hope it may help somebody.
Happy coding!