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
PHP Web Logs (BLogs)
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
Submit Site
Forex Trading Online forex trading platform

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 : 5214
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);



DB Connection Function with error handling and email failure notices
Categories : PHP, MySQL, Errors and Logging, Databases, Errors and Logging
A Custom Error Handling And Debugging Class
Categories : PHP, PHP Classes, Debugging, Errors and Logging
PHP5 Exceptions
Categories : PHP, PHP Classes, Errors and Logging
[PHP5] PHP Debugger and Helper
Categories : PHP, PHP Classes, Errors and Logging, Debugging, XML
very simple ftp class
Categories : PHP, PHP Classes, FTP
PHP Paypal IPN Integration Class v1.0.0
Categories : PHP, PHP Classes, Payment Gateways
A Timing Class
Categories : PHP, PHP Classes, Date Time
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
Create HTML forms dynamicly using Javascript & PHP
Categories : PHP, PHP Classes, Java Script
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
RSS parser. Parses RSS into an array. Quick and nasty but does the job. No checking is done for correct Tags, only correct XML. PHP4 needed to display result (uses print_r).
Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS)
Very minimal templating engine
Categories : PHP, PHP Classes, Templates
ElfReader: An ELF (Executable and Linking Format) header information in PHP. Shows how to use the UNPACK function to read data.
Categories : PHP, Linux, PHP Classes
A simple function to prevent undefined $_POST/$_GET/$_SESSION variable errors
Categories : PHP, Variables, Errors and Logging
an example of the cyberlib payment class
Categories : PHP, PHP Classes, Ecommerce, Credit Cards