13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

H<strong>and</strong>ling Errors Gracefully<br />

565<br />

You use trigger_error() as follows:<br />

trigger_error('This computer will self destruct in 15 seconds', E_USER_WARNING);<br />

H<strong>and</strong>ling Errors Gracefully<br />

If you come from a C++ or Java background, you are probably comfortable using<br />

exceptions. Exceptions allow functions to signal that an error has occurred <strong>and</strong> leave<br />

dealing with the error to an exception h<strong>and</strong>ler. Exceptions are an excellent way to h<strong>and</strong>le<br />

errors in large projects.They were adequately covered in Chapter 7,“Error <strong>and</strong><br />

Exception H<strong>and</strong>ling,” so they will not be revisited here.<br />

You have already seen how you can trigger your own errors.You can also provide<br />

your own error h<strong>and</strong>lers to catch errors.<br />

The function set_error_h<strong>and</strong>ler() lets you provide a function to be called when<br />

user-level errors, warnings, <strong>and</strong> notices occur.You call set_error_h<strong>and</strong>ler() with the<br />

name of the function you want to use as your error h<strong>and</strong>ler.<br />

Your error h<strong>and</strong>ling function must take two parameters: an error type <strong>and</strong> an error<br />

message. Based on these two variables, your function can decide how to h<strong>and</strong>le the<br />

error.The error type must be one of the defined error type constants.The error message<br />

is a descriptive string.<br />

A call to set_error_h<strong>and</strong>ler() looks like this:<br />

set_error_h<strong>and</strong>ler('my_error_h<strong>and</strong>ler');<br />

Having told <strong>PHP</strong> to use a function called my_error_h<strong>and</strong>ler(), you must then provide<br />

a function with that name.This function must have the following prototype:<br />

My_error_h<strong>and</strong>ler(int error_type, string error_msg<br />

[, string errfile [, int errline [, array errcontext]]]))<br />

What it actually does, however, is up to you.<br />

The parameters passed to your h<strong>and</strong>ler function are<br />

n The error type<br />

n The error message<br />

n The file the error occurred in<br />

n The line the error occurred on<br />

n The symbol table—that is, a set of all the variables <strong>and</strong> their values at the time the<br />

error occurred<br />

Logical actions might include<br />

n Displaying the error message provided<br />

n Storing information in a log file<br />

n Emailing the error to an address<br />

n Terminating the script with a call to exit

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!