PHP 8.5.0 Beta 3 available for testing

get_exception_handler

(PHP 8 >= 8.5.0)

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

説明

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

参考

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top