nutballs

I am creating my

PHP

  skeleton and was wondering how I need to do something.

When I include a class, and instantiate it, obviously the code for the class gets loaded and adds its little bit of processing time to the code base of the site.

But what about when I include the class, but never instantiate it on the page that is executing? Does the class use up any processing time (other than the first time if there is any caching going on)?

What if I include dozens of classes that never actually get used on the page? bad, or no effect?

the reason why is because I like using a single include that includes all my others. that way, when I add a new include, I don't need to edit every single page, just the "binder".

make sense?

perkiset

Yup. There are actually many different answers to your question.

In all cases there seems to be a "first pass" over whatever you include - if it's horribly corrupt then

PHP

  will cough... but if you have a syntax error halfway down it won't catch it until it actually runs the code... so this leads me to believe that code is never really compiled until it is really used.

include and include_once will bring in a reference to code with no requirement ie., if the file doesn't exist it will continue on.
require and require_once will bring in a reference to the code WITH requirement ie., if it doesn't exist it's a fatal error.

There is, of course, a small penalty for including something regardless of compilation, but if you never use the code I don't think you'll take too much of a hit. The best way, of course, is to use the APC cache so that everything is compiled and pre-linked - you can then include your butt off and take virtually no hit at all. This is how my stuff works because I have lots of classes that I want available all the time.

Another option (if you're worried about it) is conditional inclusion. You can do this:


<?

php

 
switch($aValue)
{
case 0:
require_once('./classA.

php

 ');
case 1:
require_once('./classB.

php

 ');
break;
case 2:
require_once('./classC.

php

 ');
break;
}
?>


Note here if the value is 1 then both classA and classB will be included because the switch is not broken. You can also do this:

require_once($_GET['modulename']);

where in this case a query parameter on the URL is actually specifying what the

PHP

  will include. Clearly not for the faint of heart.

Did you know that you can reference functions the same way?

<?

php

 
$functionName = 'doSomething';
$buff = $functionName()

function doSomething() { return 'Hello World'; }
?>


Last but not least is the eval($aCodeBlock) function which will execute the code contained in the aCodeBlock var. That's wonderful, but will not stay compiled in APC so you take a hit for it.

Wow. About 19 million more lines than you asked for. Must be good coffee this morning. Applause

DangerMouse

Not directly related to your question, but theres an interesting design pattern I've been trying to use lately described as 'lazy loading' where and object though initialised doesnt really do anything until its called, keeping the memory footprint low, quite useful for DB connections.

DM

jammaster82

Isnt that kind of what MMC and

.NET

  does?


Perkiset's Place Home   Politics @ Perkiset's