热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

我可以捕获exit()和die()消息吗?-CanIcatchexit()anddie()messages?

Idliketobeabletocatchdie()andexit()messages.Isthispossible?Imhopingforsomethings

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.

我希望能够捕获die()和exit()消息。这是可能的吗?我希望得到与set_error_handler和set_exception_handler类似的东西。我查看了register_shutdown_function(),但它似乎不包含违规的die()和exit()调用的上下文。

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.

我意识到die()和exit()是处理错误的不好方法。我不希望别人告诉我不要这样做。:)我正在创建一个通用系统,如果有人(而不是我)认为这是一个好主意,那么我希望能够优雅地记录exit()和die()。

7 个解决方案

#1


8  

Yes you can, but you need ob_start, ob_get_contents, ob_end_clean and register_shutdown_function

可以,但是需要ob_start、ob_get_contents、ob_end_clean和register_shutdown_function

function onDie(){
    $message = ob_get_contents(); // Capture 'Doh'
    ob_end_clean(); // Cleans output buffer
    callWhateverYouWant();
}
register_shutdown_function('onDie');
//...
ob_start(); // You need this to turn on output buffering before using die/exit
@$dumbVar = 1000/0 or die('Doh'); // "@" prevent warning/error from php
//...
ob_end_clean(); // Remember clean your buffer before you need to use echo/print

#2


7  

According to the PHP manual, shutdown functions should still be notified when die() or exit() is called.

根据PHP手册,在调用die()或exit()时仍然应该通知关机函数。

Shutdown functions and object destructors will always be executed even if exit() is called.

即使调用了exit(),关闭函数和对象析构函数也将始终执行。

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().

似乎不可能将状态发送到exit($status)。除非您可以使用输出缓冲来捕获它,但是我不确定您如何知道何时调用ob_start()。

#3


5  

Maybe override_function() could be interesting, if APD is available

如果APD可用,那么override_function()可能会很有趣

#4


4  

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.

就我所知,这是不可能的。这里发布的一些解决方案可能是有效的,但是它们需要很多额外的工作或许多依赖项。没有一种方法可以轻松而可靠地捕获die()和exit()消息。

#5


1  

If you use the single point of entry method. (index.php) I can recommend this for your error handling:

如果使用单点入口方法。(index.php)对于您的错误处理,我建议您这样做:

Short version:

短版:

ob_start();
register_shutdown_function('shutdownHandler');

include('something');

define(CLEAN_EXIT, true);

function shutdownHandler() {
    if(!defined("CLEAN_EXIT") || !CLEAN_EXIT) {
        $msg = "Script stopped unexpectedly: ".ob_get_contents();
        //Handle premature die()/exit() here
    }
}

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.

大概是我的方法。我有比这里展示的更多的东西(处理数据库事务/回滚/发送电子邮件/写日志/显示友好的错误消息/用户错误报告/等等),但这是所有这些的基本思想)。希望它能帮助一些人。

\s*([^<>].*)error<\/b>:(.*)
$/', $buffer, $matches) ) { $msg = preg_replace('/<.*?>/','',$matches[2]); //Handle Fatal error here return "There was an unexpected situation that resulted in an error. We have been informed and will look into it." } //No fatal exception. Return buffer and continue return $buffer; }

#6


0  

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

为什么不使用自定义错误处理?如果不是,您可以始终使用LD_PRELOAD和C代码注入来捕获它:)或使用您的定制重新编译php:P

#7


-1  

yes: write a function and use that instead.

是的:编写一个函数并使用它。

function kill($msg){
    // Do your logging..
    exit($msg);
}

推荐阅读
author-avatar
sannyi
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有