How To Catch PHP Fatal Error In CodeIgniter

My last few blog post’s are result of the project that I am currently working on, and this blog is also in that series.

We needed to catch PHP Fatal Errors in the production environment and notify about it to developers, also at the same time, showing our beautiful fail whale page to user instead of ugly error or a white screen of death.

Here I will show you how to do this in CodeIgniter, though credit for this idea goes to hipertracker.

First of all you will need to setup a hook, so update your hooks.php file with following code

	$hook['pre_system'][] = array(
	    'class'    => 'PHPFatalError',
            'function' => 'setHandler',
            'filename' => 'PHPFatalError.php',
   	    'filepath' => 'hooks'
);

Now put the code shown below in PHPFatalError.php file in your applications hooks folder.

This is the simplifed version of what I have done, so please update handleShutdown function as per your needs.

Check out PHPCamp a place to share news, views and articles that are useful to PHP community.

5 thoughts on “How To Catch PHP Fatal Error In CodeIgniter

  1. where are function load_page?

    I have error, PHP Fatal error: Call to undefined function load_page()

  2. can you please help, i m having php version 5.1.6 in which error_get_last() function is not available
    so i m unable to use the given code

  3. Thanks for example.

    I think it’s possible to make some changes, like:
    register_shutdown_function(array($this, ‘handleShutdown’));
    and now you can put handleShutdown() inside of PHPFatalError class =)

  4. get_instance() on a pre system hook will not always run, i’ve experienced it sadly over some cheap hosting, get_instance() returns NULL value …

    in any case, very good job.

Comments are closed.