Using the Register_shutdown_function function in PHP to intercept the fatal error example _php instance

Source: Internet
Author: User

When we do a project, we occasionally appear fatal error because of being cautious. If the display_errors is set to OFF, the user will see a blank page. If set to ON, the fatal error information will be presented (of course, normal people will not do so).

So what can we do to intercept the fatal error in advance and feed back to the user with our own custom friendly form? There is a function called Register_shutdown_function in PHP that lets you set up another function that can be invoked when execution is closed. That is, when our script executes or dies unexpectedly, and the PHP execution is about to close, this function is called.
Let's take a look at the following example:

Copy Code code as follows:

<?php
$flag = false;
function Deal_error () {
Global $flag;
if (! $flag) {
Die ("coarse question, please try again later");
}
return false;
}
Register_shutdown_function ("Deal_error");
will fail because of a fatal error
$obj = new Notexistclass (); Introducing an undefined class
Require ('./test.php ');
$flag = true;

We at the program entrance, set flag to false, and finally set to true, indicating that the program is performing normally. If the flag is not true at the end of the middle, it will call register_shutdown_function and output our custom error results.

If the above class does not define, introduce nonexistent files (must use require or require_once), etc. will cause fatal error. Of course, if your program is missing a punctuation or a special character of what, then there is no way.

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.