Use the register_shutdown_function function in PHP to intercept the fatalerror example _ php instance

Source: Internet
Author: User
This article describes how to use the register_shutdown_function function in PHP to intercept fatalerror. For more information, see fatal error. If display_errors is set to off, the user will see a blank page. If it is set to on, the fatal error information will be displayed (of course normal people will not do this ).

Then, how can we intercept the fatal error in advance and provide it to the user in our custom friendly form. PHP has a function called register_shutdown_function. We can set another function that can be called when the function is disabled. that is to say, this function will be called when PHP Execution is about to close when the script execution is completed or accidentally killed.
The following is an example:

The Code is as follows:


<? Php
$ Flag = false;
Function deal_error (){
Global $ flag;
If (! $ Flag ){
Die ("rough problem, please try again later ");
}
Return false;
}
Register_shutdown_function ("deal_error ");
// Will fail due to a fatal error
// $ Obj = new NotExistClass (); // introduce undefined classes
Require ('./test. php ');
$ Flag = true;


At the program entrance, we set flag to false, and finally to true, indicating that the program runs normally. If the flag is not true at the end, it means that it is dead somewhere in the middle. At this time, register_shutdown_function will be called to output our custom error results.

The above class is undefined and introduction of non-existent files (require or require_once must be used) will cause fatal error. Of course, if your program lacks punctuation or special characters, you can't do it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.