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:

<?php

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.