Skip to content

Instantly share code, notes, and snippets.

@MikeRogers0
Created June 16, 2012 15:42
Show Gist options
  • Save MikeRogers0/2941727 to your computer and use it in GitHub Desktop.
Save MikeRogers0/2941727 to your computer and use it in GitHub Desktop.
Handling Errors In PHP
<?php
/*
errors class – Helps management of errors in a script.
@version
1.0
@author
Mike Rogers (FullOnDesin.co.uk)
@last updated
03 June 2009
@usage
You are free to share, modify and use this code for commercial or non-commercial uses.
Please give a link back (to http://www.fullondesign.co.uk/ ) if you can, but you don't have you.
You use this at your own risk.
*/
class errors {
var $errors_data;
/*
Add the error from $new_error into an array of errors.
@param
$new_error string The text related to your error.
@return:
True – Error has been Added
@example
add_error('Username is Incorrect');
*/
public function add_error($new_error){
$this->errors_data[] = $new_error;
return TRUE;
}
/*
Outputs the errors.
@param
None
@return:
- A div (ID – error) which contains the errors.
NULL – No errors
@example
echo output_errors();
*/
public function output_errors(){
if(is_array($this->errors_data)){
// Cycle through the errors.
foreach($this->errors_data as $error) {
$return .= '<p>'.$error.'</p>';
}
// Add it to the error div
return '<div id="error">'.$return.'</div>';
}
return NULL;
}
}
// @Example – creating the class:
$errors = new errors;
// @Example – Add an error
$errors->add_error('Username is incorrect');
// @Example – Return the errors
echo $errors->output_errors();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment