0

一旦我启动了一个函数,我就会在 DB 上设置一个标志TRUE

我需要FALSE在函数的末尾设置它。

前任:

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
  }

}

如您所见,它在正常情况下可以工作,但是如果在 //OTHER CODES 部分遇到一些错误,那么它将不会执行其余代码。

该功能主要在后台工作(但不是作为命令行,它只是关闭连接并开始在后台执行)。

如何确保close_db_value()在脚本终止时执行该函数?

一些可能性

  1. 导致脚本终止的一些严重错误
  2. exit在 //OTHER CODE 部分的某处调用
  3. max_execution_time超过PHP
  4. 强杀

请帮忙。

4

2 回答 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;
    }
}
于 2013-06-26T11:41:16.360 回答
0

__destruct();每次脚本结束或卸载对象时,魔法函数也会执行。

于 2013-06-26T11:43:19.667 回答