SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,034
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)

    Cute/Clever Code

    Getting cute or clever has burned everyone. I think that's safe to say. And yet, there are still those moments when you put something in anyway because, well, it works and it's elegant.

    I've been working through error handling recently. I made the decision to build an ErrorHandler class to handle errors and house the code. At some point the thing got sorta longish (500 lines) and I thought up a means to spare the system loading it using a pair of closures.

    (This is PHP 5.3+ only)
    PHP Code:
    // Set error and exception handling.
            
    set_error_handler( function($code$message$file$line$context){
                
    ErrorHandler::setError($code$message$file$line$context);
            });
            
            
    set_exception_handler( function( \Exception $e ){
                
    ErrorHandler::exception$e );
            }); 
    Now the error handler logs notices, warnings, stricts and so on and gets firebug to put them in the console. The log is written in the master template (which handles the html head section for nearly all pages) with a single rather cute line.

    PHP Code:
    if ( class_exists('ErrorHandler'false ) ): print(ErrorHandler::getLog()); endif; 
    So I check if the ErrorHandler got loaded, and if not I don't load it (hence the rarely used second param to class_exists). If it is present then there must be at least one error. The code is in braceless syntax because all template php in my setup is braceless.

    So what cute or clever snippets have you written? Any that came back to bite you??

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,084
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Off Topic:

    I don't mean to be a killjoy, but you could just as easily define the error and exception handlers thus:

    PHP Code:
    set_error_handler( array('ErrorHandler''setError') );        
    set_exception_handler( array('ErrorHandler''exception')); 
    That will also call the functions statically (see here)

    Also, if you've never heard of it, you may want to take a look at FirePHP. I personally like that a lot better than outputting stuff to the Firebug console. Mostly because you can set levels (warning, error, etc) and it's possible to create tables.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,034
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Off Topic:

    I don't mean to be a killjoy, but you could just as easily define the error and exception handlers thus:

    PHP Code:
    set_error_handler( array('ErrorHandler''setError') );        
    set_exception_handler( array('ErrorHandler''exception')); 
    That will also call the functions statically (see here)

    Also, if you've never heard of it, you may want to take a look at FirePHP. I personally like that a lot better than outputting stuff to the Firebug console. Mostly because you can set levels (warning, error, etc) and it's possible to create tables.
    I tried that though - if you do it that way:

    PHP Code:
    set_error_handler( array('ErrorHandler''setError') );        
    set_exception_handler( array('ErrorHandler''exception')); 
    the class has to be loaded into memory. Both functions will raise a fatal error if the class isn't already present in memory - try it

    I want to avoid loading the class if not needed.

    Also, Firebug is nice and all, but error handling and output needs to work in all browsers.

  4. #4
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    but error handling and output needs to work in all browsers.
    Why exactly does server side error handling & rendering need to work in all browsers?
    <.smarter.web.development.>
    PHP Stuff: Plexus | Chocolate (BDD Framework... coming soon)
    Graphite

  5. #5
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,034
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape View Post
    Why exactly does server side error handling & rendering need to work in all browsers?
    Seriously?

  6. #6
    SitePoint Evangelist Karpie's Avatar
    Join Date
    Jul 2007
    Location
    Perth, Australia
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait, your error handler uses Firebug, as does FirePHP.... but you don't like FirePHP because it won't work in all browsers, when neither will your error handler...?

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    I tend to shoot for somewhere between clever and stupid. You get to clever an code becomes difficult to manage and come back to at a later date. You make your code stupid and it looks stupid. For example, the dumbest code possible would be all procedural but the advantages of not doing that and organizing the code far out weighs the advantages of just doing everything line by line. Than again you have the people who get to clever and code becomes very difficult to read and follow as it moves from object to object to object, etc to follow some pattern that was essentially forced upon the problem or followed blindly. Its most definitely a tough balance between sacrificing practicality and standards for a more unified whole. The one thing that does help though is comments. When you start to get clever add some comments about why your doing what your doing and you or the person looking at your code in the future will thank you. Really well written code is normally pretty clever and really poor code is normally really stupid. So it is a struggle.

  8. #8
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,034
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Karpie View Post
    Wait, your error handler uses Firebug, as does FirePHP.... but you don't like FirePHP because it won't work in all browsers, when neither will your error handler...?
    I never said there wasn't an ie fallback did I? And both Chrome and Safari now accept the console commands that firebug employs.


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
  •