Jeremy,
require_once() or include_once() will work if called within a class. Using a wrapper class, I suppose it's possible to get around it by placing the require_once() calls in the wrapper..... but it strikes me as being inefficient and more trouble than the disc access savings are worth from a code maintainablity point of view. Instead, use require() or include() calls in the "header section" of your scripts instead. Couple reasons for saying that....
First, nesting conditional include calls leads very quickly to unmaintainable code. It's easy enough to understand now when you're actively writing the project, but come back 3 months later and it won't seem quite so familiar or logical.
Second, there was a very interesting point raised at a recent PHP Users Group meeting I attended.... "Pre-optimization is the root of all evil". Basically in trying to optimize your code while you're writing it you often create code which more bloated and boggy than it would have been otherwise.
I'd suggest writing the app calling the all potentially needed include files at the script header (like #<include beer.h> calls in C) then, once the app performs properly, worrying about trying to optimize the number of disc access calls. If you're only calling 4 or 5 files conditional includes are not going to make much, if any, noticeable difference anyways. If you're calling a large enough number of included files to make a difference, there's the root of the problem anyways. Try merging related files to reduce calls.
My third reason for saying that goes back to my previous comment about class scope. As you know, the *_once() functions build a table of files already called which they compare all subsequent calls against to make sure a file is included only once. Now, image you have two separate classes (neither extends the other) which under a given set of circumstances will both need a file. Using require_once(), the first class to be called will get the file and a reference to the file will be entered in said table. When the second class tries to call the file, require_once() will read it as having already been called and ignore the call. Since the included file's functions, variables, etc. will be restricted to the scope of the first class, the second class won't know that they exist.
Another way of approaching what you're talking about would be to use GET or POST vars to determine the conditions the script will experience. For example, continuing on with a shopping cart example (and again in the script header):
<?php
if (isset($purchaseItems)) {
require_once("cart_checkout.inc");
}
else if (isset($addItem) || isset($removeItem)) {
require_once("cart_maintainence.inc");
}
//...........
?>
This kind of conditional include is obviously much easier to maintain since it's not buried with class and function structure.
HTH.
geoff