Shutdown PHP !!!!!!!

Lakin Mohapatra
2 min readJun 30, 2017

Shutdown PHP !!!!!!! — The title may sound bit scary, don’t worry, spend few minute to read this post.

Generally PHP developers use try & catch for handling various errors and exceptions.

Let me talk about a specific scenario.

Suppose you are running a php script from command line .
You want that script to run for 5 hours only and then stop the script.
But here the challenging part is before/after halting php script , we need to call one API endpoint and send some data.

How we will do it ?

We may try different methods.
We can set maximum php execution time of 5 hours so that it can halt after 5 hours.
If curl operations are going on, we can add curl timeouts as well.

But for performing some actions before/after halting php script , we need to find a callback.

But does PHP has a callback for that ? Oh, yes, its has…continue reading…

There is a php function called register_shutdown_function() which registers a callback to be executed after script execution finishes or exit() is called.

As per php.net website ,

Multiple calls to register_shutdown_function() can be made, and each will be called in the same order as they were registered. If you call exit() within one registered shutdown function, processing will stop completely and no other registered shutdown functions will be called.

Example :

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');

How to handle fatal error ?
======================
As we know , if we will set max php execution time of 50 seconds , it will show one fatal error i.e
“Maximum execution time of 50 seconds exceeded”.

So we need to handle that error and perform something.

/**

* Handling fatal error

*

* @return void

*/

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))

{

# Here we handle the error, displaying HTML, logging, …

echo ‘There is an error has occured in ‘ . $error[‘file’];

// Perform API calls .

}

}

# Registering shutdown function

register_shutdown_function(‘fatalErrorHandler’);

There is another way to handle fatal error.

function fatal_error() {
if ( ! defined(PROGRAM_EXECUTION_SUCCESSFUL)) {
// fatal error has occurred
}
}
register_shutdown_function('fatal_error');define('PROGRAM_EXECUTION_SUCCESSFUL', true);

I like the simplicity of this function.
When everything else is done, it kicks into action. It’s clean, and gives you a lot of control over what needs to be done just after execution of php script.

Know more information about various php error constants from here.
http://php.net/manual/en/errorfunc.constants.php

Know more about php shutdown function from here http://php.net/manual/en/function.register-shutdown-function.php.

Reference :
http://stackoverflow.com/questions/4410632/handle-fatal-errors-in-php-using-register-shutdown-function

--

--

Lakin Mohapatra

Software Engineer | Hungry coder | Proud Indian | Cyber Security Researcher | Blogger | Architect (web2 + web 3)