PHP Error handling
php
Solution
I would ultimately think it sweetness to somehow register the error handler with PHP so that I would just need to throw the error and then decide what to do with it and whether to continue.
You can do exactly that, with set_error_handler() and set_exception_handler().
There is no "one right way" to do error handling, but here are some things to consider.
- trigger_error() is similar to throw new Exception in that they both escape out of the current execution immediately, only trigger_error() is not catchable.
- How do you want errors handled on a dev environment (show on screen?) versus in a production environment (logged and emailed?)
- You can use the above functions to essentially "convert" errors into exceptions, or vice versa
- Not all error types can be handled with a custom error handler
Problem
Thank you one and all ahead of time. I am currently in the process of tweaking/improving a MVC framework I wrote from scratch for my company. It is relatively new, so it is certainly incomplete. I need to incorporate error handling into the framework (everything should have access to error handling) and it should be able to handle different types and levels of errors (User errors and Framework errors). My question is what is the best way and best mechanism to do this? I know of PHP 5 exception handling and PEAR's different error mechanism, but I have never used any of them. I need something efficient and easy to use. Would it be better to create my own error handling or use something already made? Any suggestions, tips, questions are certainly welcomed. I would ultimately think it sweetness to somehow register the error handler with PHP so that I would just need to throw the error and then decide what to do with it and whether to continue. EDIT: Sorry, I should of provided a more details about what type of errors I wanted to log. I am looking to log 2 main types of errors: User and Framework. For user errors, I mean things like bad urls (404), illegal access to restricted pages, etc. I know I could just reroute to the home page or just blurt out a JavaScript dialog box, but I want to be able to elegently handle these errors and add more user errors as they become evident. By Framework errors I means things like cannot connect to the database, someone deleted a database table on accident or deleted a file somehow, etc. Also, I will take care of development and live server handling.