pear, generic error class?
| From: | Tom Fishwick | Date: | Fri, 07 Apr 2000 08:06:22 +0000 |
| Subject: | pear, generic error class? | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-13269@lists.php.net to get a copy of this message | ||
Hi!
Just wondering if pear might include a generic error class that would be
used with the classes that are included in pear? Instead of each class
having it's own unique way of dealing with errors.
I made an error class a while ago that has worked well for me so far,
although it needs rewriting now that I know better what I want. Here's
a little blurb I starting writing about it:
class Error { }
A generic error class to be used by a class. It knows about three types
of errors, a message, warning, and error. A message would be something
like 'Your query returned no data', a warning might be 'the email
address you entered doesn't contain an @ symbol', and an error is bad,
example, mysql_connect() fails, or mysql_query fails (maybe the sysadmin
suddenly decided to turn off magic_quotes).
I decided on the msg,warn,error types so I could do something useful
with them, such as emailing me error messages, and logging warnings to a
file (if many people make the same error when filling out a form, I
should add a note on the form or something).
The msg, warn,and error functions all return false, and are meant to be
used like:
include "error.php";
class Test
{
var $this->error; // instance of class Error
function Test()
{
$this->error = new Error;
}
function get_file($myfile)
{
if(!file_exists($myfile))
return $this->error->msg("File '$myfile' doesn't exist");
return file($myfile);
}
}
$test = new Test;
if(!$myfile = $test->get_file("/my/file.txt"))
echo "Oops! ".$test->error->str;
else
echo $myfile[0];
Any reason why I can't do:
file_exists($myfile) or return($this->error->msg("File '$myfile' doesn't
exist"));
that gives me an error??? it would be _much_ cleaner though...
Tom