classErrorLog.php

Overview:

The purpose of this class is to collect error messages and other notices so that all may be displayed at once in an orderly manner at some desired location on a web page. As discussed in the main documentation section, a switch should be set so that messages display only during testing, not in visitors' browsers.

The following steps illustrate instantiation and use of the class:

Invocation:

// Do this at start-up and not inside a function:
require("classErrorLog.php");
$errorLog=new ErrorLog();

Set catastrophic error message:

// If inside a function, declare global
global $errorLog;
$fileName="configFile.txt"; // or other vital file
$errorLog->fileNotFound($fileName);

Set non-critical message:

$msg="New config.db file created";
$errorLog->setErrorMsg($msg);

Display list of all messages:

print($errorLog->dumpErrorLog();

Complete source listing of classErrorLog.php:

<?
  /*
    filename:     classErrorLog.php
    author:       Al Olmstead, AlOlmstead.com, Al@AlOlmstead.com
    dependencies: none
  */
class ErrorLog {
  var $errorLogArray;
    function setErrorMsg($errorMsg) {
      $errorLogArrayCount=count($this->errorLogArray);
      $this->errorLogArray[$errorLogArrayCount]=
        trim($errorMsg);
      } // setErrorMsg()
    function dumpErrorLog() {
      $returnLine="";
      $errorLogArrayCount=count($this->errorLogArray);
      if($errorLogArrayCount>0) {
        $returnLine='<h5 style="margin: 0; padding: 0; '.
          'text-align: left;">Notices:</h5>';
        for($x=0; $x<$errorLogArrayCount; $x++) {
          $returnLine.='<p style="margin: 0; padding: 0; '.
          'margin-left: 10px; margin-right: 2px;'.
            'font-size: x-small; text-align: justify; '.
            'text-indent: -10px;">'.
            $this->errorLogArray[$x]."</p>";
          } // for()
        } // if()
      return($returnLine);
      } // dumpErrorLog()
    
  function fileNotFound($fileName) {
    $errorMsg='File "'.$fileName.
      '" cannot be found. This problem prevents this '.
      'website from functioning correctly. Please '.
      'notify the Webmaster.';
    $this->setErrorMsg($errorMsg);
    } // fileNotFound()
  
  function __construct() {
    $this->errorLogArray=array();
    } // __construct()
  } // class ErrorLog{}
?>