我可以捕获 exit() 和 die() 消息吗?
我希望能够捕获 die()
和 exit()
消息。这可能吗?我希望有类似 set_error_handler
和 set_exception_handler
的东西。我查看了 register_shutdown_function()
但它似乎不包含有问题的 die()
和 exit()
调用的上下文。
我意识到 die()
和 exit()
是处理错误的糟糕方法。我不希望被告知不要这样做。 :) 我正在创建一个通用系统,并且希望能够优雅地记录 exit()
和 die()
如果由于某种原因有人(不是我)认为这是一个好主意。
I'd like to be able to catch die()
and exit()
messages. Is this possible? I'm hoping for something similar to set_error_handler
and set_exception_handler
. I've looked at register_shutdown_function()
but it seems to contain no context for the offending die()
and exit()
calls.
I realize that die()
and exit()
are bad ways to handle errors. I am not looking to be told not to do this. :) I am creating a generic system and want to be able to gracefully log exit()
and die()
if for some reason someone (not me) decides this is a good idea to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的,可以,但您需要 ob_start、ob_get_contents、ob_end_clean 和 register_shutdown_function
Yes you can, but you need ob_start, ob_get_contents, ob_end_clean and register_shutdown_function
根据 PHP 手册,在死亡时仍应通知关闭函数( ) 或 exit() 被调用。
似乎无法获取 exit($status) 中发送的状态。除非您可以使用输出缓冲来捕获它,但我不确定您如何知道何时调用
ob_start()
。According to the PHP manual, shutdown functions should still be notified when die() or exit() is called.
It doesn't seem to be possible to get the status sent in exit($status). Unless you can use output buffering to capture it, but I'm not sure how you'd know when to call
ob_start()
.如果 APD 可用,也许 override_function() 可能会很有趣
Maybe override_function() could be interesting, if APD is available
据我所知,这实际上是不可能的。此处发布的一些解决方案可能有效,但它们需要大量额外的工作或许多依赖项。没有办法轻松可靠地捕获 die() 和 exit() 消息。
As best as I can tell this is not really possible. Some of the solutions posted here may work but they require a lot of additional work or many dependencies. There is no way to easily and reliable trap the die() and exit() messages.
为什么不使用自定义错误处理呢?如果没有,您始终可以使用 LD_PRELOAD 和 C 代码注入来捕获它:) 或者使用您的自定义重新编译 php :P
Why do not use custom error handling instead? If not, you could always use LD_PRELOAD and C Code injection to catch it :) Or recompile php with your customizations :P
如果使用单点进入方法。 (index.php)我可以推荐这个用于您的错误处理:
简短版本:
其他步骤和更详细:
大致是我的做法。我所做的事情比我在这里展示的还要多(处理数据库事务/回滚/发送电子邮件/编写日志/显示友好的错误消息/用户错误报告/等),但这是所有这些背后的基本思想)。< br>
希望它能帮助某人。
If you use the single point of entry method. (index.php) I can recommend this for your error handling:
Short version:
Additional steps and more detailed:
Roughly my way of doing it. I have even more going on than I show here (handling database transactions/rollback/sending e-mails/writing logs/displaying friendly error messages/user error reporting/etc), but this is the basic idea behind all of it).
Hope it helps someone.
捕获退出在自动化测试中很有用。我这样做的方法是抛出一个特殊的运行时异常,而不是直接调用 exit。
Catching exits is useful in automated tests. The way I do it is I throw a special runtime exception instead of calling exit directly.
是的:编写一个函数并使用它。
yes: write a function and use that instead.