set_exception_handler funksiyası
set_exception_handler funksiyası tutulmamış istisnaları emal etmək üçün çağırılacaq funksiyanı təyin etməyə imkan verir. Parametr kimi emaledicinin funksiya adı və ya anonim funksiya ötürülür. Emaledicı parametr kimi istisna obyektini alır.
Sintaksis
set_exception_handler(callable $exception_handler): callable
Nümunə
Sadə bir istisna emaledicisi təyin edək:
<?php
function myExceptionHandler($exception) {
echo 'Tutulan istisna: ' . $exception->getMessage();
}
set_exception_handler('myExceptionHandler');
throw new Exception('Nəsə düz getmədi!');
?>
Kodun icra nəticəsi:
'Tutulan istisna: Nəsə düz getmədi!'
Nümunə
Emaledicı kimi anonim funksiyanın istifadəsi:
<?php
set_exception_handler(function($exception) {
echo 'Xəta: ' . $exception->getMessage();
});
throw new Exception('Kritik xəta');
?>
Kodun icra nəticəsi:
'Xəta: Kritik xəta'
Nümunə
Əvvəlki emaledicinin bərpası:
<?php
function firstHandler($exception) {
echo 'Birinci emaledicı: ' . $exception->getMessage();
}
function secondHandler($exception) {
echo 'İkinci emaledicı: ' . $exception->getMessage();
}
set_exception_handler('firstHandler');
$old_handler = set_exception_handler('secondHandler');
restore_exception_handler(); // Birinci emaledicini bərpa edir
throw new Exception('Test');
?>
Kodun icra nəticəsi:
'Birinci emaledicı: Test'