Skip to content

Instantly share code, notes, and snippets.

@norv
Created December 26, 2012 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norv/4380530 to your computer and use it in GitHub Desktop.
Save norv/4380530 to your computer and use it in GitHub Desktop.
<?php
/**
* This class is an experiment for the job of handling errors.
*/
class error_context
{
/**
* Holds the unique identifier of the error (a name).
*
* @var string
*/
private $_name = null;
/**
* An array that holds all the errors occurred separated by severity.
*
* @var array
*/
private $_errors = null;
/**
* The default severity code.
*
* @var mixed
*/
private $_default_severity = 0;
/**
* A list of all severity code from the less important to the most serious.
*
* @var array/mixed
*/
private $_severity_levels = array(0);
/**
* Certain errors may need some specific language file...
*
* @var array
*/
private $_language_files = array();
/**
* Multipleton. This is an array of instances of error_context.
* All callers use an error context ('post', 'attach', or 'default' if none chosen).
*
* @var array of error_context
*/
private static $_contexts = null;
/**
* Initialize the class
*
* @param string error identifier
* @param array/mixed a list of all severity code from the less important to the most serious
* @param mixed the default error severity code
*/
private function __construct ($id, $severity_levels, $default_severity)
{
if (!empty($id))
$this->_name = $id;
if (!empty($severity_levels))
$this->_severity_levels = $severity_levels;
if (!empty($default_severity))
$this->_default_severity = $default_severity;
else
$this->_default_severity = array_shift(array_values($this->_severity_levels));
}
// ...
public static function context($id = 'default')
{
if (self::$_contexts === null)
self::$_contexts = array();
if (!array_key_exists($id, self::$_contexts))
self::$_contexts[$id] = new error_context($id);
return self::$_contexts[$id];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment