What is the best method in PHP for the Error Handling ?
is there a way in PHP to Convert all PHP errors (warnings notices etc) to exceptions ?
what the best way/practise to error handling ?
again:
if we overuse exceptions (i.e. try/catch) in many situations, i think application will be halted unnecessary.
for a simple error checking we can use return false; but it may be cluttering the coding with many if else conditions.
what do you guys suggest ?
1
If your development and production environments are separate, you should have the display_errors
ini setting set to off
on production, and on
(and I would set the error_reporting
level to -1
: all errors) on development. It shouldn’t be too difficult to write code that doesn’t emit any warnings or notices through normal usage unless you’re using a PEAR library, as those have a lot of legacy code that causes warnings.
As for errors and exceptions, on development you should just treat them normally so you can actually see what’s wrong, and on production you can use the set_error_handler
and set_exception_handler
methods to catch errors and exceptions and display an error page to the user instead and write the actual error to a log file that you can check later.
Other than that, I wouldn’t use any special tactics for error suppression.
When you develop your code you should have all the error reportings in PHP enabled, including notices and warnings. Your production code should never produce a PHP Error. In some special cases you may survive with some PHP Warnings and Notices, but on your production server you should only show the Errors to the user.
Next, PHP Errors mean that you have something really messed up in your code and such things should never reach production. Any amount of decent automated tests will make sure this never happens. => Correction for clarity: “… will make sure this happens rarely.”
I don’t recommend you to transform PHP Errors, Warnings and Notices into Exceptions or “FALSE”. Never! If they happen on your computer the application has a serious problem, so don’t transform the error into something that you can catch and than forget / ignore easily.
When you have situations where your code can go wrong, use proper verification and throw exceptions. Even more, whenever possible make your own exception and throw the one most appropriate for that situation.
For example, instead of letting PHP throw an “Invalid index” when you try to read an element of an array, you may do something like:
if (!isset($superWeapons['nuclear']))
throw new MissingSuperweaponException('We heave no nuclear weapons awailable!');
Than, somewhere at a higher level of your application you can catch all these exceptions and show the user the nice messages you want.
Please note, that exceptions are for exceptional cases, for situations in which your application should never be. Use then only when needed.
When there is a result of an operation, a result which can be TRUE or FALSE or a value or a string, that is not an exceptional case, that is a result or a return value. For example, when you do a search for a string the result can be the index of the searched text. However, not finding the text is not an exceptional case, you should just return zero or false, not an exception.
4
Short Answer: If you use PHP 5, you can handle error with exceptions:
https://www.php.net/manual/en/class.exception.php
This way is cleaner than manual set exception message, because you have access to a try catch system and you can isolate exception handling