2

According to the manual http://php.net/manual/en/function.register-shutdown-function.php, register_shutdown_function() is implemented as:

<?php
function shutdown()
{
    // This is our shutdown function, in 
    // here we can do any last operations
    // before the script is complete.

    echo 'Script executed with success', PHP_EOL;
}

register_shutdown_function('shutdown');
?>

Could it be implemented as an anonymous function? If so, how?

1 Answer 1

5

Anonymous function are simply passed as an argument:

register_shutdown_function(function(){
    // Code
});

Note that this applies to every function, not only to register_shutdown_function();

The only differences between using a named function vs an anonymous one, is if you need to call this function outside the context of register_shutdown_function, at this point you need to use a named function (altho this is a rare edge case).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks dynamic. Easier than I expected! Any reason not to do so?
can anyone elaborate on "outside the context of register_shutdown_function" with an example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.