The PEAR Package Tour: PEAR Basics - Core PEAR Classes
(Page 4 of 5 )
PEAR includes a few core classes which are used by most packages to standardize the interface. These include a class for interacting with PEAR itself, error and exception management, and a System class which provides improved cross-platform support for file system operations.
Of these classes, the most important is PEAR and its error handling. This is the primary behavior that you’ll need understand and deal with when consuming PEAR packages in you applications. Nearly every function or method in a PEAR package that can fail, will utilize the PEAR error handling. This is done through the PEAR base class and the raiseError() method.
function pearMethod() {
…
if ($somethingWentWrong) {
return PEAR::raiseError(‘Something went wrong’);
}
}
Calling code should check the returns form PEAR modules with the isError() method.
$result = pearMethod();
if (PEAR::isError($result)) {
die(‘Uh Oh! ‘ . $result->getMessage());
}
This allows a programmer to always know how to handle possible errors from the PEAR packages. For recent PHP5 only packages, PEAR provides an exception class with a similar purpose.