質問

捕まえられるようになりたいです die() そして exit() メッセージ。これは可能でしょうか?私は同様のものを期待しています set_error_handler そして set_exception_handler. 。見てきました register_shutdown_function() しかし、問題を引き起こす文脈が含まれていないように見えます die() そして exit() 呼び出します。

把握した die() そして exit() エラーを処理する悪い方法です。私はこれをするなと言われるのを望んでいるわけではありません。:) 私は汎用システムを作成しており、正常にログを記録できるようにしたいと考えています。 exit() そして die() 何らかの理由で誰か (私ではない) がこれを行うのが良い考えだと判断した場合。

役に立ちましたか?

解決 4

私はこれは本当に不可能です言うことができるように最善のよう。ここに掲載ソリューションの一部が動作するかもしれませんが、彼らは追加の仕事や多くの依存関係の多くを必要としています。簡単かつ信頼性の高いトラップダイ()とexitへの道()メッセージます。

はありません

他のヒント

はい、できますが、必要です 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

による PHPマニュアル, シャットダウン関数は、die() または exit() が呼び出されたときに通知される必要があります。

シャットダウン機能 そして オブジェクトデストラクタ exit() が呼び出されても常に実行されます。

exit($status) で送信されたステータスを取得することはできないようです。出力バッファリングを使用してキャプチャできる場合を除きますが、いつ呼び出すかをどうやって知るかわかりません ob_start().

たぶん override_function()のAPDが利用可能な場合は、面白いかもしれません

なぜ、代わりにカスタムのエラー処理を使用していませんか?そうでない場合、あなたは常にそれをキャッチするLD_PRELOADとCコードインジェクションを使用することができます:)またはカスタマイズして再コンパイルPHP:P

単一エントリポイント方式を使用する場合。(index.php) エラー処理にはこれをお勧めします。

短縮版:

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
    }
}

追加の手順と詳細:

おおよそ私のやり方です。ここで示したもの以外にも、さらに多くの処理が行われています (データベース トランザクションの処理/ロールバック/電子メールの送信/ログの書き込み/わかりやすいエラー メッセージの表示/ユーザー エラーの報告/など)。ただし、これがすべての背後にある基本的な考え方です)。
誰かの役に立てば幸いです。

<?php

    //Some initialization

    //starting output buffering. (fatalErrorHandler is optional, but I recommend using it) 
    ob_start('fatalErrorHandler');

    //Execute code right at the end. Catch exit() and die() here. But also all other terminations inside PHPs control
    register_shutdown_function('shutdownHandler');

    //handling other errors: Also optional
    set_error_handler('errorHandler');



    try {
        //Include of offensive code
        include(...);
    }
    catch (Exception $ex) {
        //Handling exception. Be careful to not raise exceptions here again. As you can end up in a cycle. 
    }

    //Code reached this point, so it was a clean exit.
    define(CLEAN_EXIT, true);


    //Gets called when the script engine shuts down.
    function shutdownHandler() {

        $status = connection_status();

        $statusText = "";

        switch ($status) {
            case 0:
                if (!defined("CLEAN_EXIT") || !CLEAN_EXIT) {
                                    $msg = "Script stopped unexpectedly: ".ob_get_contents();
                    //Handle premature die()/exit() here
                }
                else {
                    //Clean exit. Just return
                    return;
                }
            case 1: $statusText = "ABORTED (1)"; break;
            case 2: $statusText = "TIMEOUT (2)"; break;
            case 3: $statusText = "ABORTED & TIMEOUT (3)"; break;

            default : $statusText = "UNKNOWN ($status)"; break;
        }

        //Handle other exit variants saved in $statusText here ob_get_contents() can have additional useful information here
    }

    // error handler function  (This is optional in your case)
    function errorHandler($errno, $errstr, $errfile, $errline) {

        $msg = "[$errno] $errstr\nOn line $errline in file $errfile";

        switch ($errno) {
            case E_ERROR:               $msg = "[E_ERROR] ".$msg;               break;
            case E_WARNING:             $msg = "[E_WARNING] ".$msg;             break;
            case E_PARSE:               $msg = "[E_PARSE] ".$msg;               break;
            case E_NOTICE:              $msg = "[E_NOTICE] ".$msg;              break;
            case E_CORE_ERROR:          $msg = "[E_CORE_ERROR] ".$msg;          break;
            case E_CORE_WARNING:        $msg = "[E_CORE_WARNING] ".$msg;        break;
            case E_COMPILE_ERROR:       $msg = "[E_COMPILE_ERROR] ".$msg;       break;
            case E_COMPILE_WARNING:     $msg = "[E_COMPILE_WARNING] ".$msg;     break;
            case E_USER_ERROR:          $msg = "[E_USER_ERROR] ".$msg;          break;
            case E_USER_WARNING:        $msg = "[E_USER_WARNING] ".$msg;        break;
            case E_USER_NOTICE:         $msg = "[E_USER_NOTICE] ".$msg;         break;
            case E_STRICT:              $msg = "[E_STRICT] ".$msg;              break;
            case E_RECOVERABLE_ERROR:   $msg = "[E_RECOVERABLE_ERROR] ".$msg;   break;
            case E_DEPRECATED:          $msg = "[E_DEPRECIATED] ".$msg;         break;
            case E_USER_DEPRICIATED:    $msg = "[E_USER_DEPRICIATED] ".$msg;    break;
            default:                    $msg = "[UNKNOWN] ".$msg;               break;
        }

        //Handle Normal error/notice/warning here. 
        $handled = ...

        if ($handled)
            return true; //handled. Proceed execution
        else
            throw Exception($msg); //Be careful. this might quickly become cyclic. Be sure to have code that catches and handles exceptions. Else die() here after logging/reporting the error.

    }

    function fatalErrorHandler(&$buffer) {

        $matches = null;
        //Checking if the output contains a fatal error
        if (preg_match('/<br \/>\s*<b>([^<>].*)error<\/b>:(.*)<br \/>$/', $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;
    }

はい:。その代わりに、機能と使用を書く

function kill($msg){
    // Do your logging..
    exit($msg);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top