Many programs already use the PEAR package. Many php programmers are more or less familiar with error handling in PEAR. But this mechanism is not limited to the PEAR package-everyone can use these methods in their classes and programs.
This article is divided into two parts: first, we will look at the functions used for error processing in the class, and then we will look at how to handle Errors Based on the PEAR error processing mechanism.
Our example class is cvs2db, which inserts data from a CSV file into a database table. Because the data may be hand-written, their data should be verified before being inserted-implement postcode. The Function import () completes reading, checking, and inserting; it returns the number of corrupted records. If the returned value is greater than 0, the error record set can be written to the new CSV file using exportUnvalid. The typical usage is as follows:
$ Cd = new csv2db ();
$ Dsn = MySQL (the best combination with PHP): // root @ localhost/csv2db;
If (0 <$ cd-> import ("./dat.csv", $ dsn, address )){
$ Cd-> exportUnvalid ("./dat2.csv ");
}
?>
Possible errors include:
The CSV file to be imported does not exist,
Failed to connect to database,
The record set is damaged, and the exported CSV file cannot be created.
In the classic solution that provides error information, you may write the following code:
$ Cd = new csv2db ();
$ Dsn = MySQL (the best combination with PHP): // root @ localhost/csv2db;
$ Result = $ cd-> import ("./dat.csv", $ dsn, address)
Switch ($ result ){
Case FILE_NOT_OPENED:
...
Break;
Case DATABASE_ERROR:
...
Break;
Default:
If (0 <$ result ){
$ Cd-> exportUnvalid ("./dat2.csv ");
} Else {
Echo every thing OK!
}
}
?>
This is acceptable for short scripts and is also a common method-but not for large programs that are often concerned about error handling. The traditional possibility forces the author of the class to make the final decision! In most cases, this decision is based on calls to classes at that time rather than the idea of long-term use and reusable code. A flexible Error handling mechanism is an important part of reusable code. The PEAR Error API is such a well-tested mechanism.
Class in user's eyes
In addition to the two functions, the class provides a set of error handler functions and a special localized error message feature called DB2CVS_Error.
Now I will show you how to control the class behavior when an error occurs.
Local and global error handling
You use setErrorHandling () to manage error handling. This function requires two parameters: the first is the error mode, and the second (optional) parameter is the option specific to the error mode. For example, setErrorHandling (PEAR_ERROR_PRINT, This error occurred % s) and setErrorHandling (PEAR_ERROR_TRIGGER, E_USER_WARNING ).
The call method of this function is the most important in general behavior: static or entity. In the cvs2db class, both of them can be used to set error handling. All these calls have the same structure-set error mode for the class:
// Per instance
$ Cd = new csv2db ();
$ Cd-> setErrorHandling (PEAR_ERROR_DIE ):
// Static
CVS2DB: setErrorHandling (PEAR_ERROR_DIE );
PEAR: setErrorHandling (PEAR_ERROR_DIE );
If the two give the same result, what is the difference? Object calls are only set for that class, while static calls work for all classes that use PEAR_Error or derive from that class. This also applies to the first static command CVS2DB: setErrorHandling (PEAR_ERROR_DIE) -- although it seems to only affect the cvs2db class.
Summary: using commands as an object function means only setting the error mode for this object (local), and calling as a static function sets the error mode (global) for the entire script ).
SetErrorHandling () and raiseError ()
Both functions can be called statically and as functions of entities. It is important to remember how a combination influences each other.
Basically, the static call of setErrorHandling () only affects the static call of raiseError () -- setErrorHandling () as the object function, and only affects raiseError () as the static function call. In csv2db, it is not feasible to use csv2db: setErrorHandling () to set the error mode, because we use $ this-> raiseError (...). One tips to solve this problem -- rewrite raiseError ():