Another option is to use the following function:
function register_named_shutdown_function($name, $callback)
{
static $callbacks = [];
$callbacks[$name] = $callback;
static $registered;
$registered || $registered = register_shutdown_function(function () use (&$callbacks) {
array_map(function ($callback) {
call_user_func($callback);
}, $callbacks);
}) || true;
}
Sample Usage:
register_named_shutdown_function("first", function () {
echo "Never executed\n";
});
register_named_shutdown_function("second", function () {
echo "Secondly executed\n";
});
register_named_shutdown_function("first", function () {
echo "Firstly executed\n";
});
Outputs:
Firstly executed
Secondly executed
source
share