Conditional Class Declaration – bad practice?

By | | PHP

6

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.

Written By:

Harry Fuecks

Harry has been working in corporate IT since 1994, with everything from start-ups to Fortune 100 companies. Outside of office hours he runs phpPatterns: a site dedicated to software design with PHP that aims to raise standards of PHP development. He also maintains Dynamically Typed: SitePoint's PHP blog.

 

{ 6 comments }

HarryF April 1, 2004 at 6:47 am

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

HarryF April 1, 2004 at 6:46 am

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

Alan Knowles March 31, 2004 at 9:20 pm

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

HarryF March 31, 2004 at 3:51 pm

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

George Schlossnagle March 31, 2004 at 2:23 pm

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.

Winged Spider March 31, 2004 at 2:05 pm

[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

Comments on this entry are closed.