Fatal error:php through the Register

Source: Internet
Author: User

Today found in PHP, if you want to record fatal error, you can use a good function,
Called Register_shutdown_function, summary as follows:
Register_shutdown_function allows us to set a second function that can be invoked when execution is closed. That is, our function will be invoked when our script executes or dies unexpectedly, causing PHP execution to close. So, We can use the method of setting a variable to false at the beginning of the script and then setting it to true at the end of the script, so that PHP closes the callback function to check whether the script is complete or not. If our variable is still false, we know that the last line of the script has not been executed, so it must have been executed at the execution of the program;
Examples are as follows:

 
  1. ? php
  2. $clean = false;
  3. function Shutdown_func () {
  4. Global $clean;
  5. if (! $clean) {
  6. Die ("Not a clean shutdown");
  7. }
  8. return false;
  9. }
  10. Register_shutdown_function ("Shutdown_func");
  11. $a = 1;
  12. $a = new Fooclass (); will fail because of a fatal error
  13. $clean = true;
  14. ?>
  15. One more example of recording fatal error
  16. function Catch_fatal_error ()
  17. {
  18. $last _error = Error_get_last ();
  19. Check to see if it belongs to Fatal_error
  20. if (isset ($last _error[' type ']) && $last _error[' type ']==e_error)
  21. {
  22. Related processing
  23. }
  24. }
  25. Register_shutdown_function (' Catch_fatal_error ');


Note that;
The 1,register_shutdown_function () function can be called repeatedly, but the order of execution is the same as the order of registration
2, if there is an exit () function call before the Register_shutdown_function () function is invoked, the register_shutdown_function () function will not execute
Support for registration function parameter passing after 3,PHP4
4, in some service terminals, such as Apache, the current directory can be changed in the Register_shutdown_function () function
5,register_shutdown_function () function executes after headers is sent

This article links http://www.cxybl.com/html/wlbc/Php/20130318/37221.html

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.