7.3. PEAR Errors

PEAR has its own error-reporting mechanism based around the principle of errors as types, and the ability to pass around errors as values. Many extras were built around this principle, to the point where PEAR errors almost function like a poor man's (in this case, PHP 4 users') exception.

Where PHP's built-in error mechanism typically displays a message and a function returns false, a function returning a PEAR error gives an object back that is an instance of PEAR_Error or a subclass:


require_once 'DB.php';

$dbh = DB::connect('mysql://test@localhost/test');
if (PEAR::isError($dbh)) {
    die("DB::connect failed (" . $dbh->getMessage() . ")\n");
print "DB::connect ok!\n";


In this introductory example, we try connecting ...

Get PHP 5 Power Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.