For want of Exceptions
I've been over this issue for a few days now and have effectivly confused myself as to what to do. I've been designing a large library for use on my website. This is the first time I've done anything on this scale in PHP, so I'm a bit lost as to what to do about error handling.
In other languages, I'd create a heirarchy of Exception objects. But PHP4 doesn't have exception handling, nor can I convince my webhost to install PHP5 to get the genine article. I had a few ideas that get close:
Define a function called try(), and call this function at the beginning of a troublesome block of code. try() would call set_error_handler(), and then ob_start(). The script would resume processing until a function called catch(). catch() would check if an error occured and then call ob_clean(), and output a page reporting the error instead. If no error occured, set_error_handler() is reset to the handler before try() was called, and then ob_flush() would output the HTML generated by the script.
There is a problem with this, of course: when you actually throw an error, the script will continue executing despite the error. I could output error HTML and then call die() when and error is thrown, but this wouldn't work for my site. My php code typically starts in the middle of the page, and if I call die() in the middle of it, the remaining HTML wouldn't be closed properly and wouldn't display correctly (and then, what's the point?).
Any ideas on how to handle errors in this sort of situation?
In other languages, I'd create a heirarchy of Exception objects. But PHP4 doesn't have exception handling, nor can I convince my webhost to install PHP5 to get the genine article. I had a few ideas that get close:
Define a function called try(), and call this function at the beginning of a troublesome block of code. try() would call set_error_handler(), and then ob_start(). The script would resume processing until a function called catch(). catch() would check if an error occured and then call ob_clean(), and output a page reporting the error instead. If no error occured, set_error_handler() is reset to the handler before try() was called, and then ob_flush() would output the HTML generated by the script.
There is a problem with this, of course: when you actually throw an error, the script will continue executing despite the error. I could output error HTML and then call die() when and error is thrown, but this wouldn't work for my site. My php code typically starts in the middle of the page, and if I call die() in the middle of it, the remaining HTML wouldn't be closed properly and wouldn't display correctly (and then, what's the point?).
Any ideas on how to handle errors in this sort of situation?
