#native_company# #native_desc#

PEAR Primer Page 3

By Joe Stump
on December 26, 2002

The PEAR_Error Class

The PEAR_Error class is really easy to use. It’s a very basic class that serves a very basic purpose, error checking
and error reporting. I now return nothing but PEAR_Error’s in my classes, instead of using true/false. Why? I’m not
only able to tell easily if my class is in error, but I’m also able to pass along an error message for easy debugging.
You’ll note in the above class that I assign a new instance of PEAR_Error to $this when an error occurs. This means that
if an error occurs while connecting to the database the class that you originally created as an instance of Base will
now be an instance of PEAR_Error, not allowing you to do any Base related operations on that instance. Here’s a
quick example:


  $base =& new Base();






The above code would create an instance of Base, but since Base is an abstracted class it would error out
with “Base is an abstracted class!” There are three main things to note about PEAR_Error when you are
using it. The first is that the constructor takes your error message as its first parameter. The second is that the
function isError() is part of the PEAR class and can be used in any child class with the scope operator ::. The last
is the getMessage() function is used to get your error message. But since the classes we write are perfect we
won’t be needing this, will we?