1

Once i initiated a function i am setting a flag on DB to TRUE.

and i need to set it FALSE on the end of the function.

Ex:

Class Preprocessor extends Machine{

  function process(){
    $this->db->setValue(TRUE); //Setting DB flag to TRUE

    //OTHER CODES

     $this->close_db_value(); // Setting DB flag to FALSE

  }

  function close_db_value(){
    $this->db->setValue(FALSE); //Setting DB flag to FALSE
  }

}

As you can see that it will work in normal cases, But if some error encountered in //OTHER CODES section then it will not execute the rest of codes.

The function mainly work in background (But not as a command line,its just closing the connection and start executing on background).

How can i ensure that the close_db_value() function executed upon script termination ?

Some of possibilities

  1. Some critical errors that which leads the script to termination
  2. exit called somewhere on the //OTHER CODE section
  3. PHP max_execution_time exceeded
  4. A Force Kill

Please help.

2
  • 1
    A try { ... } finally { ... } block should always execute the finally block, no matter if there was a fatal error or not. I have no idea if it will execute in the latter three cases though. See the documentation. Commented Jun 26, 2013 at 11:46
  • @Sumurai8 Its not, The last one is the problematic one. The fact is that the script may run about 1 Hour on background and my hosting provider dont like this. :) Commented Jun 26, 2013 at 11:49

2 Answers 2

2

Use the set_error_handler() function in PHP. Place the name of the callback function you want called in case of error as the first parameter. Put the level of error (optional) that you want as the trigger as the second parameter.

If you are looking to handle every other type of error (except SIGKILLs or force quits), you can also try using register_shutdown_function() with a error_get_last() and a switch-case statement with the various types of errors you wish to handle (E_NOTICE, E_DEPRECATED, etc.).

For example,

register_shutdown_function('shutdown_callback');

function shutdown_callback(){
    $error_type = error_get_last();
    switch($error_type){
        case E_ERROR: break;
        case E_WARNING: break;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.
@Red I just modified my answer to also add an alternative way, after doing some light research. Also, register_shutdown_function()'s documentation specifies how to handle a SIGTERM/SIGKILL if you need that handled as well, but I've never bothered with that.
Thanks, But in fact i can handle the first 2 listed possible terminations register_shutdown_function() will fail if the timeout is exceeded.Is there any system (OS) level solution ?
0

The __destruct(); magic function will also execute every time the script ends, or an object is unloaded.

Comments

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.