Home  >  Article  >  Backend Development  >  Briefly describe the example of catching exception interrupt in PHP

Briefly describe the example of catching exception interrupt in PHP

巴扎黑
巴扎黑Original
2017-08-16 10:33:581510browse


Summary: When an abnormal situation occurs in the PHP program, such as a fatal error, timeout, or unknown logical error that causes the program to interrupt, register_shutdown_function can be used at this time. Exception handling.

When an abnormal situation occurs in the PHP program, such as a fatal error, timeout, or unknown logical error that causes the program to be interrupted, register_shutdown_function can be used for exception handling.

#For example, to determine whether a script has been executed, you can set an attribute to false, set it to true when the execution is completed, and finally use the method specified by the register_shutdown_function function to determine , and do further exception handling, as shown in the code:





##










PHP



In this way, you can quickly locate whether the script is interrupted, handle exceptions through register_shutdown_function and improve the robustness of the program, and record the status of program interruption, making it easy to quickly locate problems through logs. register_shutdown_function execution mechanism

Quote:

PHP transfers the function to be called into memory. This function is called when all PHP statements on the page have been executed. Note that at this time it is called from memory, not from the PHP page, so if there is path information, the absolute path should be used, because PHP has already assumed that the original page does not exist. There is no relative path at all.

You can understand the calling conditions like this:
1. When the page is forced to stop by the user
2. When the program code runs out of time
3. When the PHP code is executed When completed


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23


class IndexController extends Controller

{

    /**

* Whether the script execution is completed

* @var bool

*/

    protected $complete = false ;

    public function __construct()

    {

        register_shutdown_function([$this, 'shutdown']);

    }

 

    /**

* Exception handling

*/

    public function shutdown()

    {

        if ($this->complete === false) {

          dump('www.tanteng.me'); //Logs should be output and exception handling operations should be performed here

                                                                                                                                                                                                                  Logs should be output and exception handling operations should be carried out.

##   

}

}

The above is the detailed content of Briefly describe the example of catching exception interrupt in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn