PHP Development School: A closer look at Pear's error handling_PHP Tutorial

WBOY
Release: 2016-07-13 17:33:47
Original
709 people have browsed it


Many programs already use PEAR packages. Many php(as the current mainstream development language) programmers are more or less familiar with error handling in PEAR. But this mechanism is not limited to PEAR packages - anyone 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 handling in the class, and then we will look at how to handle errors based on the PEAR error handling mechanism.
 
Our example class is called cvs2db, which inserts data from a CSV file into a database table. Because the data may be handwritten, their data should be verified before insertion - implement postcode. The function import() completes the work of reading, checking and inserting; it returns the number of damaged records. If the returned value is greater than 0, the faulty recordset can be written to a new CSV file using exportUnvalid(). Typical usage is like this:
 
 (as the current mainstream development language)
 $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:
 
 To The imported CSV file does not exist,
failed to connect to the database,
the record set is damaged, and the CSV export file cannot be created.
 
In a classic solution for providing error messages you might write code like this:
 
 (as the current mainstream development language)
 $ 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 and common for short scripts - but for larger scripts where error handling is often a concern This is not the case with the program. The traditional possibility forces the class author to make the final decision! In most cases, the decision is based on the call to the class at that time rather than on long-term usage and reusable code. A flexible error handling mechanism is an important part of reusable code, and the PEAR Error API is one such well-tested mechanism.
 
 Class in the eyes of users
In addition to those two functions, the class provides a set of error handling functions and an own error object called DB2CVS_Error, which has a special Localized error messages feature.
 
Now I will show you how to control the behavior of a class when an error occurs.
 
 Local and global error handling
You manage error handling with setErrorHandling(); this function takes two parameters: the first is the error mode, and the second (optional) ) parameters are error mode specific options. For example, setErrorHandling(PEAR_ERROR_PRINT, This error occurred %s) and setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_WARNING).
 
 The method of calling this function is the most important in general behavior: static or entity. In the class cvs2db, we can use both to set up error handling. All these calls have the same structure - set the error mode for the class:
 
 // per instance
 $cd = new csv2db( );
$cd->setErrorHandling(PEAR_ERROR_DIE):
// static
CVS2DB::setErrorHandling(PEAR_ERROR_DIE); Both give the same result, what's the difference? Entity calls are set only for that class while static calls work for all classes that use PEAR_Error or are derived from that class. This also affects the first static command CVS2DB::setErrorHandling(PEAR_ERROR_DIE) - although it appears to only affect the cvs2db class.
 
Summary: Using the command as an entity function means setting the error mode only for this entity (locally), while calling it 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's important to remember how a combination affects each other.
 
Basically: the static call of setErrorHandling() only affects the static call of raiseError() - setErrorHandling() as an entity function only affects the raiseError() as a static function call.In class csv2db, using csv2db::setErrorHandling() to set the error mode is not feasible because we use $this->raiseError(...). Wen Tian has a little trick to solve this problem - rewrite raiseError():

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508600.htmlTechArticleMany programs already use PEAR packages. Many PHP (as the current mainstream development language) programmers are more or less familiar with error handling in PEAR. But this mechanism is not limited to PEA...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!