PHP Development Academy: A closer look at Pear's error handling _php tutorial

Source: Internet
Author: User
Tags pear
Many programs already use the Pear package. A lot of PHP (as the current mainstream development language) programmers are more or less familiar with the error handling in pear. But this mechanism is not limited to pear packages--Everyone can use these methods in their classes and programs.
  
This article is divided into two sections: first we'll look at the functions used in the class for error handling, and then we'll 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 table in the database. Since the data may be handwritten, their data should be validated before inserting--implement postcode. The function Import () completes the work of reading, checking, and inserting; it returns the number of corrupted records. If the value returned is greater than 0, an error recordset can be written to the new CSV file using Exportunvalid (). The typical usage is this:
  
   $CD = new csv2db ();
$DSN = MySQL (the best combination of PHP collocation)://root@localhost/csv2db;
if (0 < $CD->import ("./dat.csv", $DSN, address)) {
$CD->exportunvalid ("./dat2.csv");
}
?>
  
Possible errors include the following:
  
The CSV file to import does not exist,
Connection to database failed,
Recordset corruption, and the CSV export file could not be created.
  
In the classic solution that provides the error message, you might write code like this:
  
   $CD = new csv2db ();
$DSN = MySQL (the best combination of PHP collocation)://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 an acceptable and common approach for short scripts-but not for large programs where error handling is often a concern. The traditional possibility of forcing the author of the class to make the final decision! In most cases, this decision is based on the idea of a call to the class at that time rather than on long-term use 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 the user
In addition to those two functions, the class provides a set of error-handling functions and one of its own error objects called Db2cvs_error, which has a special localized error message for the feature feature.
  
Now I'll show you how to control the behavior of a class 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 error mode specific option. For example, seterrorhandling (Pear_error_print, this ERROR occurred%s) also has seterrorhandling (Pear_error_trigger, e_user_warning).
  
The way this function is called is the most important of the general behavior: Static or entity. In class cvs2db, we can use both to set error handling, all of which have the same structure-set the error pattern 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, where is the difference? Entity invocation is only for that class setting and static invocation works 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 appears to affect only the Cvs2db class.
  
Summary: Using a command as an entity function means that the error pattern is set only for the entity (local), and the call as a static function is to set the error mode (global) for the entire script.
  
Seterrorhandling () and RaiseError ()
  
All two functions can be called statically and function as entities. It's important to remember how a combination makes them interact with each other.
  
Basically: the static invocation of seterrorhandling () only affects the static invocation of RaiseError ()--seterrorhandling () as the entity function only affects raiseerror () as a static function call. In class csv2db, it is not feasible to use csv2db::seterrorhandling () to set the error mode because we use the $this->raiseerror (...). There's a little trick to solving this xuwu-rewriting raiseerror ():

http://www.bkjia.com/PHPjc/508600.html www.bkjia.com true http://www.bkjia.com/PHPjc/508600.html techarticle Many programs already use the Pear package. A lot of PHP (as the current mainstream development language) programmers are more or less familiar with the error handling in pear. But this mechanism is not confined to the pea ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.