Error handling mechanism for PHP
PHP error Handling is more complex, this article explains all errors related to PHP in the important knowledge points to do a comb, easy to understand PHP error mechanism.
Basic knowledge
Before you do, familiarize yourself with the basics of PHP error
Pre-defined constants
Run-time configuration
Abnormal
Error handling functions
Pre-defined constants
Defines all PHP error-type constants, each of which is an integer value that
The above value (numeric or symbolic) is used to create a bits mask that sets the error message to be reported. You can use bitwise operators to combine these values or to mask some types of errors. Please note that in php.ini, only ' | ', ' ~ ', '! ', ' ^ ' and ' & ' will parse correctly.
From the point of view of use, can be divided into three categories:
Manually thrown by the user
E_USER_NOTICE, E_USER_WARNING, E_USER_ERROR,E_USER_DEPRECATED
User-caused
E_NOTICE, E_PARSE, E_WARNING, E_ERROR, E_COMPILE_ERROR, E_COMPILE_WARNING, E_STRICT,E_RECOVERABLE_ERROR
caused by the PHP kernel
E_CORE_ERROR,E_CORE_WARNING
From the point of view of terminating program execution, there are two types of
Terminating program execution
program termination, enter process error process
Do not terminate program execution
Generates an error, but the program can continue to execute, as well as enter the error handling process
For the type of error in PHP, refer to this more detailed article--php Error mechanism summary
Run-time configuration
Manual-run-time configuration explained in detail, but several configurations still require special attention
error_reporting
Report the type of error, recommended in a dev/test environment configured E_ALL to resolve all types of errors after configuration in the production environment E_ALL & E_DEPRECATED , indicates: report all errors except discarded errors
display_errors
Whether an error is displayed, configured to False in the production environment, and with error_reporting the above settings, indicates that all errors except the deprecation error are reported, but the error message is not displayed.
log_errors
If the error record is turned on, the production environment needs to be turned on. In conjunction with the above two configurations, it indicates that all errors except the discarded error are reported, the error message is not displayed, but the record (only PHP can manipulate the error message) into the log.
error_log
Specifies the wrong file (Syslog is a special value). The default is not set, in the manual:
If the configuration is not set, the error message is sent to the SAPI error logger
In general, the error log that is logged to Apache/nginx is not set. In conjunction with the above three configuration, it indicates that all errors except the deprecation error are reported, the error message is not displayed, but logged to the Apache/nginx log. If the file path is configured, all errors except the discarded error are reported, and no error message is displayed, but logged file_dir in the log .
The above configuration affects the most basic performance of PHP errors. Of course, these configurations can be changed by ini_set() changing or php-fpm the configuration in code
Error handling functions
There are not many error functions, and the most concern is that set_error_handler set_exception_handler they can be involved in the error/exception handling process.
As mentioned above, after an error occurs, an error-handling process is made, and how is the error process defined?
Let's take a look at the tutorial in the PHP Manual: Errors
Simply put, the default process is done through configuration, but we can set a custom error-handling process
How to handle errors that terminate script execution
As mentioned above, there are two types of errors, and how do you deal with this error that terminates the execution of the script?
set_error_handlerThis error cannot be handled, which is easily overlooked. So look for another way.
This problem is basically accomplished (not yet seen in other scenarios):
Terminating a script error terminates the execution of the script//calls the handler that has been registered through Register_shutdown_function//To register our error-handling process so that it enters the custom error process Register_shutdown_ function (' Fatalerrorhandle '); Fatalerrorhandle (Array $error = null) { ... if (null = = = $error) { //The last error can be obtained in this way $error = Error_get_last (); } ... Log or other logic}
Abnormal
According to the explanation of w3cphp exception handling:
Exception handling is used to change the normal process of a script when a specified error (exception) condition occurs. This condition is called an exception.
When an exception is triggered, it usually occurs:
Current code state is saved
Code execution is switched to the pre-defined exception handler function
Depending on the situation, the processor may restart execution of code from the saved code State, terminate the script execution, or continue executing the script from somewhere in the code
An exception that is not caught terminates the execution of the script and produces a e-error error, performs the defined exception handling, and, if none, makes the default PHP error-handling process, which is recorded in the log. However, in programming concepts, exceptions and errors should be separated, exceptions are predictable for the user and do not conform to the expected, controllable structure.
The above mentioned set_exception_handler is the handling of exceptions, usage and set_error_handler consistency. Exception handling in each framework is very mature, mostly in the set_exception_handler Exception transfer to the framework can be processed level, the framework will also open good interface for users to use, so as to achieve the purpose of user control exception handling, Achieve customization and expansion.
Summarize
The error handling mechanism of PHP is always ignored, but it has a great effect on debugging and monitoring errors. This article mainly introduces the main points of knowledge, and makes a comb, hoping to be useful to everyone. Please see the Manual for more details.
Learning materials
Pre-defined constants
Run-time configuration
Error handling functions
Summary of PHP error mechanisms
Abnormal
Errors
PHP Exception Handling
Symfony Debug: Is a complete application, can be said to be a comprehensive tutorial, all the error-related knowledge points are involved. It is recommended to read the source code.