HomePHP Page 6 - Building A Generic Error Reporting Class In PHP
Running On Empty - PHP
The traditional method of building dynamic, PHP-based Web sites - mixing HTML elements with PHP code - can result in mangled Web pages (and much user angst) if errors take place during script execution. But yes, you can avoid the ugliness - plug in our handy error reporting class, which provides a simple way of trapping script errors and generating consistent, user-friendly error screens.
The first thing the constructor does is initialize the error stack, via the flushErrors()
method. Here's what this method looks like:
// clean stack
Once that's done, the constructor uses the ob_start() function to define a special
output buffer which stores all the output generated by the script during its lifetime. When I do this, the output of the script is never seen by the user unless I explicitly make the contents of this buffer visible via a call to PHP's output control API.
Once a buffer has been defined, the script proceeds to execute as usual. When you've decided that it's time to display the contents of the buffer to the user, you can simultaneously end output buffering and send the contents of the current buffer to the browser. Alternative, you can also clear the contents of this buffer at any time via a call to ob_clean() (as you will see on the next page, this is the function I will be using to clear and re-draw the screen when an error occurs).