4

In this question about including all the classes in a directory for an interpreter that needs all of them, it was suggested that a better way to handle the problem would be to conditionally include only the Command file that is needed for this iteration of the interpreter. For example, instead of this:

require_once('CommandA.php');
require_once('CommandB.php');
require_once('CommandC.php');

class Interpreter {

    public function interpret($action) {
        switch($action) {
            case 'A':
                $c = new A();
                $c->execute();
                break;
        }
    }
}

Do something more like this:

class Interpreter {

    public function interpret($action) {
        switch($action) {
            case 'A':
                require_once('CommandA.php');
                $c = new A();
                $c->execute();
                break;
        }
    }
}

What are the advantages and disadvantages of going with the second choice -- dynamically and conditionally loading only the needed command -- as opposed to the first one -- including all of the files at the head of the file, C-style?

In the answer given to my previous question, speed, footprint and ease of writing were listed as advantages of conditionally loading. In other questions where this is addressed, I've seen it suggested that c-style loading is more readable and maintainable. Can anyone elaborate on these? Any other advantages or disadvantages?

Community
  • 1
  • 1
Daniel Bingham
  • 12,414
  • 18
  • 67
  • 93
  • 2
    Another alternative is to use an autoloader http://uk.php.net/manual/en/function.spl-autoload-register.php which means you don't need requires embedded in your class code or to require every possible class at the top of your scripts – Mark Baker Sep 21 '10 at 08:09
  • 1
    possible duplicate of [Will reducing number of includes/requires increase performance?](http://stackoverflow.com/questions/3423953/will-reducing-number-of-includes-requires-increase-performance) – Gordon Sep 21 '10 at 08:21
  • @Gordon I think the scope of the two questions is different, though similar. I'm asking about more than just performance. I want general advantages and disadvantages. Including readability and writability, any scoping issues, unexpected behavior, etc. – Daniel Bingham Sep 21 '10 at 09:06
  • @Mark I'll certainly look into the autoloader. – Daniel Bingham Sep 21 '10 at 09:08
  • Readability and Maintainability is clearly better when putting the includes upfront, but then again, autoloading will simply remove that concern completely. IMO as long as the files contain classes or function definitions only (which will always be imported into the global scope), the only applicable considerations are about performance. But that's just my 2c. – Gordon Sep 21 '10 at 09:12

1 Answers1

2

The main advantage of the conditional loading is that it won't include a whole tree of php files every time you load that class. Event when you use a code cache (and you should), the files are checked for updates every time the vm encounters an /(include|require)(once)?/, so including unnecessary php files will slow down your code.

The disadvantages of this particular method is, that it is harder to maintain, and easier to make mistakes. Writing one thing twice is a code smell. Wirting My_Class means, that you need this class, and writing include "My_Class.php" also means this. This is not right.

I suggest using autoloading: http://php.net/manual/en/language.oop5.autoload.php

netom
  • 3,322
  • 2
  • 21
  • 21