PEAR::setErrorHandling() – sets handling of errors generated through PEAR packages


require_once 'PEAR.php';

void PEAR::setErrorHandling ( integer $mode = null , mixed $options = null )


setErrorHandling() can be invoked as both a standard object method ($obj->setErrorHandling) and as a static method (PEAR::setErrorHandling). If called statically, PEAR::setErrorHandling() sets the default error handling behaviour for all PEAR objects (global error handling behaviour). If called as an object method, $obj->setErrorHandling() sets the default error handling for only that object (local error handling behaviour).


  • integer $mode - one of the following constants

    • PEAR_ERROR_RETURN If an error occurs, a PEAR_Error is returned from the error-generation method (normally raiseError().)

    • PEAR_ERROR_PRINT Like PEAR_ERROR_RETURN, but an error message will be printed additionally.

    • PEAR_ERROR_TRIGGER Like PEAR_ERROR_RETURN, but the PHP builtin-function trigger_error() will be called in PEAR_Error's constructor with the error message.

    • PEAR_ERROR_DIE The script will terminate and an error message will be printed on instantiation of a PEAR_Error.

    • PEAR_ERROR_CALLBACK If a error occurs, the callback passed to $options is called.

    • PEAR_ERROR_EXCEPTION If Zend Engine 2 is present, then an exception will be thrown using the PEAR_Error object.

  • mixed $options - the value for $options depends on $mode

    • PEAR_ERROR_PRINT and PEAR_ERROR_DIE support an optional printf() format string used when printing the error message. This format string should contain a single %s, which will be used to insert the error message into the string. Use the string to enclose the error message with other useful information not included in the error message prefix or error message.

    • PEAR_ERROR_TRIGGER requires an user error level constant used by trigger_error() (possible constants: E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR). Note that if the error constant is not one of these valid error constants, a PHP warning will be triggered.

    • PEAR_ERROR_CALLBACK The callback must be a function name in the format described in the Pseudo-Type section of the PHP manual (either a string, or an array). The callback must accept a single parameter, the PEAR_Error object generated by an error condition. Note that if the callback is not a valid callback, a PHP warning will be triggered.

Here's an example of a few ways to use setErrorHandling:

require_once 'PEAR.php';
// dummy error constant for this example

// demonstration of default global error handling
// in this case, all PEAR Errors will trigger a PHP warning
// Note that the file and line number will be in the constructor of PEAR_Error
// in PEAR.php
PEAR::raiseError('test warning'MYCLASS_ERROR_CODE);

// this error specifies a mode, and overrides the default global error handling

PEAR::setErrorHandling(PEAR_ERROR_PRINT"Gronk error: %s<br />\n");

// prints "Gronk error: test warning<br />\n"
PEAR::raiseError('test warning'MYCLASS_ERROR_CODE);

 * Fake class to demonstrate error handling
 * @package myClass
class myClass extends PEAR {
     * Demonstration of default local error handling
function myClass()
// object method callback
$this->setErrorHandling(PEAR_ERROR_CALLBACK, array(&$this'handleErr'));
// prints "custom working"
PEAR::raiseError('custom handler'MYCLASS_ERROR_CODE);
// static class method callback
PEAR::raiseError('custom handler'MYCLASS_ERROR_CODE);
// function callback
PEAR::raiseError('custom handler'MYCLASS_ERROR_CODE);
     * Callback set by the constructor
     * @param PEAR_Error The error object
function handleErr($error)
$this->lastError $error->getMessage();
$error->getMessage() . " working\n";
     * Static callback set by the constructor
     * Note that in PHP 5, $this is not set if the method is declared with
     * the "static" access modifier.  In PHP 4, $this is set, but is not
     * set to the myClass object, so don't use it!
     * @param PEAR_Error The error object
     * @static
function handleErrStatic($error)
'static ' $error->getMessage() . " working\n";

 * @param PEAR_Error The error object
function standardCallback($error)
'normal function callback: ' $error->getMessage();
// This causes the printing of three messages through error callbacks:
// "custom working"
// "static custom handler... is working"
// "normal function callback: custom handler"
$mine = new myClass;

// terminates the script with the error message "oops"
Create a new PEAR_Error object and optionally specify error-handling instructions (Previous) add an error code for non-disabling temporary error handling (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.