Blog Post RSS ?

Blogs » PHP » Conditional Class Declaration - bad practice?
 

Conditional Class Declaration - bad practice?

by Harry Fuecks

Was perturbed to read one on the comments on Zend’s PHP5 - Ask the Experts;

Q: In PHP 5 (rc1-dev)… is there any reason why you can’t do this?
if (!defined('SOMETHING')) { class HelloWorld { .... } }

A: This is a problem we know about. In general, we don’t recommend doing conditional declarations of classes because it isn’t a very good programming practice. However it is yet unclear, if it will be supported, and it might be.

OK - in general to declare classes conditionally, for no other good reason than because you can, is probably a bad idea.

But as I was advocating here, there are very good reasons, IMO, why conditional declaration (i.e. conditional includes) are a good idea, the #1 being so that you can reduce the code “footprint” being included on every page request, when running your app on a typical shared PHP host (where PHP accelerators generally aren’t available).

If it’s a question of declaring which files are could be included in any given script up at the top, for the sake of readability etc., a simple tip via Jason Sweat, is something like;

if ( FALSE ) { require_once 'dbconn.php'; require_once 'logger.php'; require_once 'auth.php'; // etc. }

Although none of those classes will every be loaded at this point, it makes it easily visible what might get included in the script later on, so they’re visible to other developers and easy to document with phpDocumentor.

Could be I’m over reacting to a statement that just needs more qualification but makes me nervous to see it on Zend, from one of PHP’s core developers.

If you liked this blog, share the love:

  • Save to Del.icio.us

This post has 6 responses so far

  1. [ot]I can’t view the forum thread you linked to because I don’t have enough permissions?!? Weird…

    http://www.sitepoint.com/forums/showthread.php?t=151665/ot

     
  2. I think the real bad practice it protects against is this:

    if($day == ‘Tuesday’) {
    class foo {
    // class definition …
    }
    } else {
    class foo {
    // something else
    }
    }

    That sort of confitional declaration is much more clearly bad practice, I think.

     
  3. Not to worry - it was a link to Jason’s member profile in the forums, as opposed to a specific post.

     
  4. It has been fixed now.. - There where a number of essential examples that used this.. (eg. FC/BC compatiblity on overload stuff)

     
  5. [QUOTE=Anonymous]I think the real bad practice it protects against is this:

    if($day == ‘Tuesday’) {
    class foo {
    // class definition …
    }
    } else {
    class foo {
    // something else
    }
    }

    That sort of confitional declaration is much more clearly bad practice, I think.[/QUOTE]

    Good point. I guess this is what was being referred to as bad practice.

     
  6. [QUOTE=Anonymous]It has been fixed now.. - There where a number of essential examples that used this.. (eg. FC/BC compatiblity on overload stuff)[/QUOTE]

    Thanks for the update - thats good to hear.

     

Sponsored Links

Leave a response

You are not logged in, log in with your SitePoint Forum username and password.

-OR- Post Anonymously

* Make sure any code samples are escaped (i.e. ‘<b>’ becomes ‘&lt;b&gt;’).

If not logged in, your comments will be placed in a moderation queue. This means your comment may not appear until one of our moderators approves it.

SitePoint Marketplace

Buy and sell Websites, templates, domain names, hosting, graphics and more.

Logo Design, Web page Design and more!

99designs

  • Custom logo designs created ‘just for you’.
  • Pick the design you like best.
  • Only pay if you’re satisfied with the result.