get_exception_handler

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

説明

callablenull get_exception_handler()

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

パラメータ

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

戻り値

現在定義済みの例外ハンドラを返します。 ハンドラが定義されていない場合は、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;
    }
}
?>

参考

  • set_exception_handler
  • restore_exception_handler
  • restore_error_handler
  • error_reporting
  • Exceptions