php自定义错误处理_PHP中的自定义错误处理

php自定义错误处理

Web application don't always go to plan, that much is obvious. Users will continuously find ways to trigger errors within your application and how you record, contain, and eliminate these errors is a true testament to your code. PHP provides means for trapping errors and dealing with them the way you need them to be dealt with. The best part of PHP error handling is that it's extremely customizable using PHP's set_error_handler() function.

Web应用程序并不总是按计划进行,这很明显。 用户将不断找到触发应用程序中错误的方法,以及如何记录,包含和消除这些错误,这是对代码的真实证明。 PHP提供了捕获错误并以所需的方式对其进行处理的方式。 PHP错误处理的最好部分是可以使用PHP的set_error_handler()函数进行高度自定义。

错误处理程序 (The Error Handler)

function xhandler($number,$string,$file,$line,$context)
{
	//log to text file?

	//log to xml file?

	//store in database?

	//whatever you want to do!
}

说明 (The Explanation)

Your error handling function can accept five parameters:

您的错误处理函数可以接受五个参数:

  • $number - Integer error number representative of the PHP error level

    $ number-整数错误号,代表PHP错误级别

  • $string - String description of the error

    $ string-错误的字符串描述

  • $file - File in which the error occurred

    $ file-发生错误的文件

  • $line - Line number in the file that the error occurred

    $ line-发生错误的文件中的行号

  • $context - Context of the area, including an array of each variable in scope

    $ context-区域的上下文,包括作用域中每个变量的数组

用法 (The Usage)

/* use this error for ALL (E_ALL) errors */
set_error_handler('xhandler',E_ALL);

This is a basic example of the custom error handling in PHP. The programming and methods you use to save and analyze these errors is up to you, but common methods include:

这是PHP中自定义错误处理的基本示例。 您可以使用用于保存和分析这些错误的编程和方法,但是常用的方法包括:

  • Saving the error to a database

    将错误保存到数据库
  • Saving the error information to a local file (text, xml, etc.)

    将错误信息保存到本地文件(文本,xml等)
  • Redirecting the user to another page

    将用户重定向到另一个页面
  • Absolutely nothing (I've actually seen this in other dev's code).

    绝对没有(我实际上已经在其他开发人员的代码中看到了这一点)。

Though you can do almost anything in your error handling function(s), I recommend the following for error handling functions:

尽管您可以在错误处理功能中执行几乎所有操作,但是我建议对错误处理功能执行以下操作:

  • Store the date/time of the error

    存储错误的日期/时间
  • Use more than one method of storing error logs (database, file, etc.)

    使用多种方法存储错误日志(数据库,文件等)
  • Email severe warnings/errors to yourself so you may deal with any critical problems as soon as possible

    通过电子邮件将严重的警告/错误发送给自己,以便您可以尽快处理任何严重的问题
  • Use different error handling functions for different error levels if necessary

    如有必要,对不同的错误级别使用不同的错误处理功能
  • Create a new file log daily so that your log file doesn't balloon

    每天创建一个新的文件日志,以便您的日志文件不会膨胀

How do you record PHP errors in your applications?

如何在应用程序中记录PHP错误?

翻译自: https://davidwalsh.name/custom-error-handling-php

php自定义错误处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值