Does trigger_error () in PHP become obsolete for something better in PHP5?

I used this when I wanted to cause errors in PHP based on the background of PHP4. Note. To handle these errors, I had my own set_error_handler().

if ($error) {
    trigger_error('Sorry, error has occured');
}

I cannot remember where, but once someone told me that I should โ€œuse exceptionsโ€. Since I am reviewing a lot of my old code, I decided it was time to get some good advice on my error handling.

Now that I'm using PHP5 (and a little smarter than when the old code was written), is my trigger_error()just the old way to do something, and if so, what is the best way to handle the error in PHP5?

+3
source share
2 answers

, PHP 5. , , - , , . , , .

, , - , , , .

PHP: - :

PHP 5 . , ( "" ) PHP. try, . . catch exeptions. ( , , ) catch . ( re-throw) catch.

? ( , Java, )

, . , , , . .

:. ( , , ), , set_exception_handler.

+4

- - .

PHP exceptions, , .

:

function doSomething() {
    if ($error) {
        throw new Exception('Some descriptive error message.');
    }
}

try {
    doSomething();
}
catch (Exception $e) {
    die('<p class="error">' . $e->getMessage() . '</p>');
}
+2

Source: https://habr.com/ru/post/1706772/


All Articles