« get_error_handler
restore_error_handler »

get_exception_handler

(PHP 8 >= 8.5.0)

get_exception_handler — ユーザー定義の例外ハンドラ関数を取得する

説明

function get_exception_handler(): ?callable

存在する場合に、現在の例外ハンドラ関数を返します。

パラメータ

この関数にはパラメータはありません。

戻り値

現在定義済みの例外ハンドラを返します。 ハンドラが定義されていない場合は、null を返します。

返されたハンドラは、 set_exception_handler() に渡された callable そのものです。

例

例1 get_exception_handler() の例

<?php

$handler = function (Throwable $ex) {
     echo "Exception: " . $ex::class . ": " . $ex->getMessage() . "\n";
};

var_dump(get_exception_handler()); // NULL

set_exception_handler($handler);

var_dump(get_exception_handler() === $handler); // bool(true)

?>

注意

ヒント

PHP 8.5.0 より前のバージョンでは、 この関数の機能は以下のような polyfill で提供できます:

<?php
if (!function_exists('get_exception_handler')) {
    function noop_exception_handler() {
    }
    function get_exception_handler(): ?callable {
        $handler = set_exception_handler('noop_exception_handler');
        restore_exception_handler();
        return $handler;
    }
}
?>

参考

関連キーワード:
handler   exception   ハンドラ   restore   callable   dump   noop   回復   Throwable   Exception   getMessage   ヒント   polyfill   提供   exists   return   参考   reporting   種類   Exceptions
関連する関数・ページ:
function.restore-exception-handler   function.set-exception-handler   function.get-error-handler   function.restore-error-handler   ref.errorfunc   function.xml-set-default-handler