PHPCookbook Reading Notes-Chapter 1 error handling, troubleshooting, and testing

Source: Internet
Author: User
Creating your own exception handling class PHP5 provides a fully functional exception handling class. Why do you need to customize the exception handling program? Because the default Exception Handling does not really properly handle unpredictable results. It only outputs error messages that are no different from common errors. Between standard error handling program and Exception Handling

Creating your own exception handling class PHP5 provides a fully functional exception handling class. Why do you need to customize the exception handling program? Because the default Exception Handling does not really properly handle unpredictable results. It only outputs error messages that are no different from common errors. Between standard error handling program and Exception Handling

Create your own exception ProcessingClass

PHP5 has provided complete functional exceptionsProcessingClass, why do we need to customize exceptions?ProcessingWhat about programs? Because of the default exceptionProcessingNot properly implement unpredictable resultsProcessing. It is only output and GeneralErrorNo differenceErrorInformation.

StandardErrorProcessingPrograms and exceptionsProcessingOne major difference between them is that an exception can include a cleanup routine that runs automatically when a custom exception is caught. This routine can perfectly clean up and exit elegantly.

Output stack track

PHP5 provides two for debuggingErrorDebug_backtrace () and debug_print_backtrace ()

Read and set configuration variables

// Read the configuration variable $ include_path = ini_get ('include _ path'); // set the configuration variable ini_set ('include _ path', ini_get ('include _ path '). ':/home/fezzik/php ');
Hide from users ErrorInformation

Set in php. ini

display_errors =offlog_errors     =onerror_log   = /var/log/php.error.log

You can also use the ini_set function in php to set

ini_set('display_errors', 'off');ini_set('log_errors', 'on');ini_set('error_log', '/var/log/php.error.log');
ErrorReport

You can use error_reporting to set display in PHP.ErrorIn PHP5, a newErrorLevel -- E_STRICT is used to improve the code improvement method, which is the only method not included in E_ALLErrorLevel. Therefore, we need to cover a varietyErrorType, you needErrorThe report is set to E_ALL | E_STRICT.

error_reporting(E_ALL);                // everythingerror_reporting(E_ERROR | E_PARSE);    // only major problemserror_reporting(E_ALL & ~E_NOTICE);    // everything but notices

CustomErrorProcessing

And exceptionsProcessingDifferent, customErrorProcessingCan be usedProcessingAll PHP reportsError, Implemented in the following form:

set_error_handler('pc_error_handler');function pc_error_handler($errno, $error, $file, $line) {    $message = "[ERROR][$errno][$error][$file:$line]";    error_log($message);}

Some magic constants are often usedErrorProcessingMedium

_ FILE __,__ LINE __,__ FUNCTION __,__ CLASS __,__ METHOD __

Unit Test

PHPUnit for PHP4 or PHP5

PHPUnit2 only for PHP5

SimpleTest is a fully functional unit.TestFramework

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.