PHP5 exception handling mechanism [5] - PHP5 error processing - use PEAR processing error

xiaoxiao2021-03-06  95

(3) Using PEAR to handle errors

You can also use PEAR to handle errors. When an error occurs, a pear_error object will be returned. The following code uses a static method PEAR :: ISERROR () to verify this object. If the error does happen, the returned Pear_Error object will provide all the information you need:

Pear_EtType () - The pear :: getuser subtype error Type Pear :: getuserinfo () - additional information about ire i: getcode () - the error Code (if any) error code

Here we have modified the getcommandObject () method to return a pear_error object.

Index_pear.php

// PHP 4 Require_once ("pear.php"); Require_once ('cmd_php4 / command.php'); class commandmanager {???? var $ cmddir = "cmd_php4"; ???? function getcommandObject ($ cmd) { ???????? $ PATH = "{$ this-> cmddir} / {$ cmd} .php"; ???????? if (! file_exists ($ PATH)) {???? ???????? Return Pear :: raiseerror ("Cannot Find $ PATH"); ????????} ???????? REQUIRE_ONCE $ PATH; ??????? ? if (! Class_exists ($ cmd)) {???????????? Return ???????????? Pear :: RaiseError ("Class $ cmd does not exist") ; ?????????} ???????? $ RET = new $ cmd (); ???????? IF (! is_A ($ RET, 'COMMAND') {? ??????????? Return ???????????? Pear :: raiseerror ("$ cmd is not a command"); ????????} ?? ?????? Return $ RET; ????}}?>

Pear_ERROR is both an error sign and a related specific information containing errors, which is very useful for customer code.

// PHP 4 $ mgr = new commandManager (); $ cmd = $ mgr-> getcommandObject ('realcommand'); if (Pear :: ISERROR ($ cmd)) {???? print $ cmd-> getMessage () "/ n"; ??? EXIT;} $ cmd-> execute ();?> Although a target value can make you flexibly reflect the problem in the program, it also has side effects of "pollution interface". Because the exit of the getcommandObject () method is now two, and it is an object, sometimes confusion.

PHP does not allow you to specify a type method or function to return the type of value, although this is more convenient. The getcommandObject () method is the Command object or a pear_error object. If you want to get a certain type of return value, you must verify the type of the value each time. A cautious code will be full of complex inspection conditions, if each type is tested.

The following is a PEAR :: DB customer code // php 4 request_once ("db.php"); $ db = "errors.db"; unlink ($ db); $ dsn = "sqlite: // ./ DB = DB :: Connect ($ DSN); $ crete_result = $ db-> query ("Create Table Records)") ")") ")") ")") ")") ")") ")") "); $ INSERT_RESULT = $ db-> query "INSERT INTO RECORDS VALUES"); $ query_result = $ db-> query ("Select * from records"); $ row = $ query_result-> fetchrow (db_fetchmode_assoc); Print $ ROW ['Name' ]. "/ n"; $ drop_result = $ db-> query ("Drop Table Records"); $ dB-> disconnect ();?>

The program is readily readable, the operation is clear - we choose a database, create a new data table and insert a row of records, retrieve the data, and then discard the data sheet. Let's take a look at the following use error judgment:

转载请注明原文地址:https://www.9cbs.com/read-121849.html

New Post(0)