Building A Generic Error Reporting Class In PHP - Running On Empty
(Page 6 of 9 )
Let's now begin constructing the class methods. I'll start with the constructor:
<?php
class errorReporter
{
// constructor
function errorReporter()
{
//
initialize error stack
$this->flushErrors();
// start output buffering
ob_start();
}
//
snip
}
The first thing the constructor does is initialize the error stack, via the flushErrors()
method. Here's what this method looks like:
<?php
class errorReporter
{
// clean stack
function flushErrors()
{
$this->_errors
= array();
return $this->_errors;
}
// snip
}
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).
Next: Raising An Alarm >>
More PHP Articles
More By icarus, (c) Melonfire