Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Eric

Eric Avatar



1,442


November 2005
The interface for error objects.

interface iError
{
   public function get_record_error();
   public function get_error();
   public function get_title();
   public function get_level();
   public function do_log();
   public function __construct($record, $title, $log, $error, $level);
   public function report();
}



This class you don't even have to worry about, it runs in the background from Error_Main

class Error_Finish
{
   protected $errors = array();
   protected $log_errors = TRUE;
   /**
   * Constructor Error_Finish
   * -makes the object and fills up an error spot
   * arg $error - (iError) error object
   **/
   public function __construct(iError $error)
   {
      $this -> add($error);
   }
   /**
   * Method add
   * arg $error - (iError) error object
   **/
   public function add(iError $error)
   {
      $this -> errors[] = $error;
   }
   /**
   * Destructor
   * -records errors to a file
   **/
   public function __destruct()
   {
      if(count($this -> errors) > 0 && $this -> log_errors)
      {
         $write_str = "";
         foreach($this -> errors as $error)
         {
            $level = ($error -> get_level() == ERROR)? "ERROR" : "FATAL";
            $write_str .= $error -> get_title() . ": " . $error -> get_record_error() . " [" . $level . "]\n";
         }
         $file_handle = fopen("errors.inf", "a+");
         fwrite($file_handle, $write_str);
         fclose($file_handle);
      }
   }
}



This class is really the controller for the rest, but you don't even have to work with it much besides by assigning what you want the application to do upon errors. I'll give an example of that later.

abstract class Error_Main
{
   protected static $finish;
   protected static $on_error;
   protected static $on_fatal;
   protected static $report_errors = TRUE;
   
   private function __construct()
   {

   }
   /**
   * Method make_log
   * -creates an Error_Finish class to store errors in
   * arg $error - (iError) an error
   **/
   protected function make_log(iError $error)
   {
      if(!isset(self::$finish))
         self::$finish = new Error_Finish($error);
      else
         self::$finish -> add($error);
   }
   /**
   * Method set_on_error
   * -assign function to execute when there is an error
   * arg $function - function to execute
   **/
   public static function set_on_error($function)
   {
      self::$on_error = $function;
   }
   /**
   * Method set_on_fatal
   * -assign function to execute when there is a fatal error
   * arg $function - function to execute
   **/
   public static function set_on_fatal($function)
   {
      self::$on_fatal = $function;
   }
   /**
   * Method register
   * -registers an error
   * arg $error - (iError) an error object
   **/
   public static function register(iError $error)
   {
      if($error -> do_log())
         self::make_log($error);
      if(self::$report_errors)
      {
         switch($error -> get_level())
         {
            case FATAL:
               if(isset(self::$on_fatal))
               {
                  $func = self::$on_fatal;
                  $func($error);
               }
               else
               {
                  die($error -> get_title() . ": " . $error -> get_error());
               }
            break;
            case ERROR:
               if(isset(self::$on_error))
               {
                  $func = self::$on_error;
                  $func($error);
               }
               else
               {
                  print $error -> get_error();
               }
            break;
         }
      }
   }
}



Here's the main error class. This is the main part that you will need to interact with.

define("FATAL", 2);
define("ERROR", 1);

class Error implements iError
{
   protected $record;
   protected $title;
   protected $error;
   protected $level;
   /**
   * Constructor Error
   * -Makes and reports an error
   * arg $record - (String) the logged error
   * arg $title - (String) title of the error
   * arg $error - (String) viewed error
   * arg $level - seriousness of error
   **/
   public function __construct($record, $title, $log = TRUE, $error="", $level=FATAL)
   {
      $this -> record = $record;
      $this -> title = $title;
      $this -> error = ($error == "")? $record : $error;
      $this -> level = $level;
      $this -> log = $log;
      $this -> report();
   }
   /**
   * Method get_record_error
   **/
   public function get_record_error()
   {
      return $this -> record;
   }
   /**
   * Method get_error
   **/
   public function get_error()
   {
      return $this -> error;
   }
   /**
   * Method get_title
   **/
   public function get_title()
   {
      return $this -> title;
   }
   /**
   * Method get_level
   **/
   public function get_level()
   {
      return $this -> level;
   }
   /**
   * Method report
   * -Sends error to Error_Main
   **/
   public function report()
   {
      Error_Main::register($this);
   }
   /**
   * Method do_log
   **/
   public function do_log()
   {
      return $this -> log;
   }
}



newBookmarkLockedFalling