SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Error handling in classes and external details

    Hey all. I'm building some classes and was kinda of wondering about how
    I should design the classes to handle errors if I want the classes to be
    reusable.

    When possible should I leave it up to the person using the object to do the
    error handling. Assuming that I give them the information required to determine
    that an error has taken place?


    When the object needs to keep track of errors should I instantiate an object
    from an error handling class to deal with the errors?


    Another issue is how to get details about the enviroment/application
    into a class and keep the class reusable.

    For example if I had an error class that print errors, I would need
    to know whether to use "\n" or "<br>" for line breaks, because I might
    wanna make the script usable from the command line.
    Maybe there's some other configuration variables that I will need in my classes also.
    How do I use them and keep the classes that are using them reusable?

    The error class would have to instantiate an "config" object or something
    to get the app/enviroment details wouldn't it?


    Maybe you all know of some simple examples I could look at that deal with
    theses issues? I'd like to see how these problems were solved.

    --ed

  2. #2
    killall -9 lusers
    Join Date
    Oct 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would recommend using the "trigger_error" function. That way, if someone wants to use your class in another application, they can either take the time to write their own error handling functions, or let PHP's internal error handling display/log the errors depending on their needs. It also keeps your classes from being tied to your own error-handling classes.

  3. #3
    SitePoint Member fukas78's Avatar
    Join Date
    Apr 2003
    Location
    CZ,Ostrava
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile PEAR Error Handling

    I recommend to you look inside the PEAR project.
    There is great example how to handle errors via classes.
    http://pear.php.net

  4. #4
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by fukas78
    I recommend to you look inside the PEAR project.
    There is great example how to handle errors via classes.
    http://pear.php.net
    It looks like methods in the pear library return a "PEAR_Error" object.

    If I understand the code correctly it doesn't look like you can
    check the boolean return value of a method as usual because it
    might return an object?

    That means you can't do something like the following
    because it might return an object:

    PHP Code:
    if ($obj->doSomething() )
    {   echo 
    'I love doing something!<br>';


    Is this a good thing? I like knowing that my functions will either return
    true or false or something else, but not both. Am I right in thinking this?


    I was thinking that maybe I could make an "error" member object in each class
    that needs it.

    Then maybe you could do something like this:

    PHP Code:
    if ($obj->doSomething() )
    {   echo 
    'I love doing something!<br>';
    }
    else
    {   
    $errorObj $obj->getErrorObj();

    Does that look reasonable?

    Of course I'm also gonna have to get used to the whole try/catch way of doing
    things too I guess.. ERghh..

    --ed

  5. #5
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think you use PEAR::isError() from the outside to check whether what you got back is what you expected - makes an interim solution to PHP5 and try/catch blocks.

  6. #6
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by HarryF
    Think you use PEAR::isError() from the outside to check whether what you got back is what you expected - makes an interim solution to PHP5 and try/catch blocks.

    With try/catch I'd do something like the following wouldn't I? :

    PHP Code:
    try {
        if (!
    $obj->doSomething() )
        {   throw new 
    ExceptionClass('wrong buster');
        }
    }
    catch (
    ExceptionClass $exception) {
        
        
    $exception->toString();

    I read this page of that nice article and it shows how to do it from inside
    and outside of the class.

    Generally speaking should this type of stuff be left outside of the class
    or should it be handled internally?

    Will PHP5 have any predefined exception types? From the little bit I've
    played around with python I know it does. I'm sure Java does too.

    If not there's gonna be a gzillion exception classes all over the web written for the most common
    errors by people who have no experience handling errors this way(like me ).

    I think they should at least the most common ones predefined like I/O error etc..

    --ed

  7. #7
    SitePoint Member
    Join Date
    Apr 2003
    Location
    delhi
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is a small piece of code which track the error and write the description in alog file...

    <html>

    <head>
    <title></title>
    </head>

    <body>

    <?php
    /*
    Defining the function which will be called when any error occured
    */
    function display_error($error,$error_string,$filename,$line,$symbols){
    //fopen("the location of log file",mode)
    $file= fopen("./en/emptylog/access.log",'a+');
    $dat=date('r');
    $msg="\n$dat ::: $error_string $filename $line \n";
    //Created message to write in log file about the details fo the error
    fwrite($file,$msg);
    }

    set_error_handler('display_error');

    //where ever you want to use this functionality..just include this file and set
    //set_erro_handler('functionname');
    ?>

    </body>

    </html>

  8. #8
    killall -9 lusers
    Join Date
    Oct 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem with relying on PEAR's error handling is that your scripts would be useless to anyone who doesn't have PEAR installed without a lot of modification (which is what you were trying to avoid in the first place, right?)

    Everyone who uses PHP has the PHP error functions available to them, so you won't run into any problems that way.

  9. #9
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •