WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Building a basic error handler with custom error types
Categories : PHP, PHP Classes, Errors and Logging Click here to Update Your Picture
Ben Barnett
Date : Jun 17th 2004
Grade : 4 of 5 (graded 2 times)
Viewed : 6869
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Ben Barnett
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Error Handling Class

@author Ben Barnett <ludge@spymac.com>
Code for: Setting vars, handling errors, mailing an error report and writing to an error log.

<?php
class error_handle
{
   
/**
* The constructor function for error_handle.
* sets the error handler to the custom one below
*/
function error_handle()
    {
   
   
// Sets the error handler to this one rather than default
    // Parameters are the object containing the function and the
    // function itsself. This is because the function is inside a class.
   
set_error_handler( array( &$this, 'error_handler' ) );
   
 
    }

 
/**
  * The error handler set by set_error_handler()
  *
  * Checks to see what sort of error is being triggered. It can catch any of the E_WARNING, E_ERROR and E_NOTICE as well as the
  * E_USER_* errors. You can also add your own custom types by adding them like I have done with BIG_SCARY_ERROR. Where it says to
  * add your own code, that is where you put the code to be executed when the error of that type occurs.
  *
  * @param string $error_type The type of error being handled.
  * @param string $error_message The error message being handled.
  * @param string $error_file The file where the error occurred.
  * @param integer $error_line The line where the error occurred.
  * @param string $error_context The context in which the error occurred.
  * @return boolean
  */
 
function error_handler($error_type, $error_message, $error_file, $error_line, $error_context)
  {
   switch(
$error_type)
    {
    case
E_ERROR:
    case
E_USER_ERROR:
   
// Add your own code here!
   
echo '<p class="error">'.$error_message,' (An error type ',$error_type,' occurred). The administrator has been notified. This page might not function correctly.<br /></p>'
    break;
     
     
    case
E_WARNING:
    case
E_USER_WARNING:
   
// Add your own code here!
   
echo '<p class="error">'.$error_message,' (An error type ',$error_type,' occurred). The administrator has been notified. This page might not function correctly.<br /></p>';
    break;

    case
E_NOTICE:
    case
E_USER_NOTICE:
   
// Add your own code here!
   
echo '<p class="error">'.$error_message,' (An error type ',$error_type,' occurred). The administrator has been notified. This page might not function correctly.<br /></p>';
    break;
   
    case
BIG_SCARY_ERROR:
    echo
'A *huge* scary error has been seen skulking around on line '.$error_line.' of '.$error_file.'! It\'s shouting "'.$error_message.'"';
    break;
    }
return
true;
  }

}

?>


To use this error handler, put in in a file, and include it into your script. Then, instantiate it (eg $error_handler = new error_handler() ). The whenever an error occurs, the class will catch it. To trigger your own errors do:

trigger_error('error message typed here', E_USER_ERROR);


(where the bit in capitals is the type you want to trigger).
So you can do this to trigger a custom error type:

trigger_error('I'm going to eat your PHP code!!! Bwahahahahaaa', BIG_SCARY_ERROR);



[PHP5] PHP Debugger and Helper
Categories : PHP, PHP Classes, Errors and Logging, Debugging, XML
DB Connection Function with error handling and email failure notices
Categories : PHP, MySQL, Errors and Logging, Databases, Errors and Logging
PHP5 Exceptions
Categories : PHP, PHP Classes, Errors and Logging
A Custom Error Handling And Debugging Class
Categories : PHP, PHP Classes, Debugging, Errors and Logging
This class splits the results of the query into multiple pages like what the search engine does.
Categories : PHP Classes, PHP, MySQL, Databases
SQLite PHP Database Wrapper
Categories : PHP, PHP Classes, Databases, SQLite, Beginner Guides
PHPDRAW, the php wannabe Photoshop ;-)
Categories : PHP, PHP Classes, GD image library, Arrays
XML To Array
Categories : PHP, PHP Classes, XML, Arrays
Mssql database Manager
Categories : PHP, Databases, MS SQL Server, Classes and Objects, PHP Classes
Export Excel Dynamically to Csv then to mysql
Categories : PHP Classes, Excel, PHP
AutoCMS - This class can be used to generate a basic content site.
Categories : PHP, Content Management, PHP Classes
XML easy parser
Categories : PHP, XML, PHP Classes
Email Class
Categories : PHP, Mail, PHP Classes
Link Manager for Link Exchangers
Categories : PHP, PHP Classes, Databases, MySQL, CURL
Snoopy - the PHP net client v0.1
Categories : PHP, PHP Classes