Conditional Class Declaration – bad practice?

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.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://www.ninjasonfire.com Winged Spider

    [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

  • George Schlossnagle

    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.

  • http://www.phppatterns.com HarryF

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

  • Alan Knowles

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

  • http://www.phppatterns.com HarryF

    [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.

  • http://www.phppatterns.com HarryF

    [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.