jump to navigation

Use PHP’s “register_shutdown_function()” To Handle Fatal Errors or “End-of-Script” January 23, 2012

Posted by Tournas Dimitrios in PHP.

With over 6000 build-in functions , PHP provides a flexible platform to build our web-applications . One function that deserves to be on our top-list tool-set is register_shutdown_function() . This function allows you to execute a block of code whenever your script ends — for any reason . You could imagine it as a software hook , whatever the reason your  script terminates (just finishes , die() , exit() , Fatal-Error) , a block of custom code will be executed . You can use this call to register as many shutdown functions as you want , and they will get executed in the order that they get applied . But please stay focused on the purpose it was developed for , just a bit of extra code . The  functionality of your script must remain (be executed) in the main body of the PHP code .

Use of this function is very straight-forward :

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;


The examples on php.net shows that you should pass in a string (or array) representing a function to call . So , if you wanted to call a function “testfunction()” , you would call –> register_shutdown_function(“testfunction”) . In case the function is member of a Class “method” , then you would call register_shutdown_function(“ClassName” , “methodName” ) And lastly ,  you can pass a lambda function in for callback :

register_shutdown_function(function() {
 A code to execute


Let’s see some practical examples :

//On production environments

function fatalErrorHandler() {
# Getting last error
$error = error_get_last();
# Checking if last error is a fatal error
 if(($error['type'] === E_ERROR) || ($error['type'] === E_USER_ERROR)|| ($error['type'] === E_USER_NOTICE)) {
 # Here we handle the error, displaying HTML, logging, ...
 echo "ERRORnr : " . $error['type']. " |Msg : ".$error['message']." |File : ".$error['file']. " |Line : " . $error['line'];
    }else {
	echo "no error where found " ;


# Registering shutdown function

// let force a Fatal error -- function does not exist

You probably already know of set_error_handler() , which allows you to catch the basic errors and warnings . For uncatched exceptions , you also have set_exception_handler() . However, none of these features enable you to log fatal errors . A solution could be register_shutdown_function (the example on previous code-snippet ) .

Another example , visitors tracking :

function startLogging() {
$logfile= 'customersLog.log';
$logdetails=  date("F j, Y, g:i a") . ': ' . '<a href="http://dnsstuff.com/tools/city.ch?ip='.$_SERVER['REMOTE_ADDR'].'">'.$_SERVER['REMOTE_ADDR'].'</a>';
$fp = fopen($logfile, "a");
fwrite($fp, $logdetails);
fwrite($fp, "

# Registering shutdown function

These where just a couple examples that demonstrated the basic concepts . Hopefully this article will lead to someone sleeping better at nights . If you want to look at a library that handles a lot of these stuff for you automatically, check out the PHP error handler library by Ferenc Kovacs . This source repository also has some examples and demos you can use to test how such error handling works .



1. samehramzylabib - January 24, 2012

Reblogged this on Sam's PHP How-To.

2. lehroy - February 4, 2013

I am using this utlilty for catch fatal error it’s wrking fine

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s