PHP:错误处理的控制流程是什么? [英] PHP: What is the flow of control for error handling?

查看:151
本文介绍了PHP:错误处理的控制流程是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,所以如果这看起来很傻,我很抱歉。我搜索过,找不到任何具体解释我正在寻找的东西。

I'm new to PHP so I apologize if this seems silly. I've searched around and couldn't find anything that explained specifically what I'm looking for.

最终我有两个目标。


  1. 在生产中,发生意外错误时,向用户显示默认的oops页面。

  2. 发生预期错误时,处理它没有PHP死亡。

我的PHP错误处理的心理模型足够多,我无法向前推进任何信心和我没有找到关于这个过程的任何好的文档。

My mental model of PHP error handling is spotty enough that I can't move forward with any confidence and I haven't been able to find any good documentation about the process.

给出一些有些例子。


  1. 用户发出请求,连接到DB失败,我们显示一个oops消息。

  2. 用户发出请求,脚本不正确解析,我们显示一个oops消息。

  3. 用户发出请求,我们使用乐观锁定查询具有更新的数据库。它失败了,所以我们通知用户对象已被更新。

我认为我的大部分困惑是围绕什么错误导致脚本死亡,什么错误(使用默认处理程序),脚本死机时,我们如何优雅地通知用户?

I think most of my confusion is surrounding what errors result in the script dying and what errors do not (using the default handler), and when the script dies, how do we gracefully inform the user?

另外,标准PHP函数/对象使用异常?如果我选择以更多的C风格来处理异常,我不会在任何时候感到惊讶? PHP6会有变化吗?如果是这样,我会提出使用c风格和异常之间的差异的文章,但如果不是,我宁愿在PHP5中始终使用c风格。这不是一个问题,我有兴趣解决,除非我绝对需要。

Also, do any of the standard php functions/objects use exceptions? If I choose to handle exceptions in more of a C style I'm not going to be surprised at any point am I? Is this going to change in PHP6? If so I'll put forth the effort to paper over the differences between using c style and exceptions, but if not, I'd rather just use the c style consistently throughout PHP5. That is not a problem I am interested in solving unless I absolutely need to.

编辑:
我只是意识到内容不太匹配的标题。我想知道,当发生错误时,PHP的逻辑流程是什么?这样,我可以更好地了解如何在PHP中实现我的目标。

edit: I just realized the content doesn't quite match the title. I would like to know, when an error occurs, what is the logic flow for PHP? This way I can better understand how to go about achieving my goals with respect to error handling in PHP.

推荐答案

PHP的大部分内置在tuff触发错误,您在默认设置中无法真正处理。

most of PHP's built in tuff triggers errors that you can't really deal with in a default setup.

然而,您可以通过设置自定义错误处理程序并抛出异常而解决此问题,而不是错误。 (PHP可能会在处理内部错误之前运行你的处理程序,所以你可以通过正常的方式实际捕获异常。)

You can however workaround this by setting up a custom error handler and throwing an exception instead of the error. (PHP will, when possible, run your handler before handling the error internally, so you can actually catch Exceptions in the normal way.)

我写了一堆代码可以在我的答案中用于这种情况:
PHP:异常vs错误? a>

I've written a bunch of code you could use for this situation in my answer here: PHP: exceptions vs errors?

如果php无法实际解析您的文件,那么您几乎完全不了解,在大多数无法解析文件的情况下,php会非常崩溃。虽然您可以尝试编写自己的包含功能 eval 'sa文件包含它,只要跳过,如果 eval 失败。您需要确保您可以基本上信任文件。

If php can't actually parse your file you're pretty much screwed, php will crash pretty hard under most circumstances where it can't parse a file. Though you could try writing your own include function that eval's a file before including it and just skips if eval failed. You'll need to be sure you can basically trust the files though.

这篇关于PHP:错误处理的控制流程是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆