Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including a whole directory in PHP or Wildcard for use in PHP Include?

I have a command interpreter in php. It lives inside the commands directory and needs access to every command in the command file. Currently I call require once on each command.

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

class Interpreter {
    // Interprets input and calls the required commands.
}

Is there someway to include all of these commands with a single require_once? I have a similar problem many other places in my code (with factories, builders, other interpreters). There is nothing but commands in this directory and the interpreter needs every other file in the directory. Is there a wildcard that can be used in require? Such as:

require_once('*.php');

class Interpreter { //etc }

Is there any other way around this that doesn't involve twenty lines of include at the top of the file?

like image 605
Daniel Bingham Avatar asked Sep 21 '10 07:09

Daniel Bingham


People also ask

What is include () in PHP?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

What is __ DIR __ in PHP?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.

How do I include two PHP files?

The include() statement is used to include a php file in another file. This way you can write a piece of code in a php file and can use it to multiple files through include() statement.


3 Answers

foreach (glob("*.php") as $filename) {
    require_once $filename;
}

I'd be careful with something like that though and always prefer "manually" including files. If that's too burdensome, maybe some refactoring is in order. Another solution may be to autoload classes.

like image 143
deceze Avatar answered Sep 21 '22 13:09

deceze


You can't require_once a wildcard, but you can programmatically find all the files in that directory and then require them in a loop

foreach (glob("*.php") as $filename) {
    require_once($filename) ;
}

http://php.net/glob

like image 39
Fanis Hatzidakis Avatar answered Sep 24 '22 13:09

Fanis Hatzidakis


Why do you want to do that? Isn't it a better solution to only include the library when needing it to increase speed and reduce footprint?

Something like this:

Class Interpreter 
{
    public function __construct($command = null)
    {
        $file = 'Command'.$command.'.php';

        if (!file_exists($file)) {
             throw new Exception('Invalid command passed to constructor');
        }

        include_once $file;

        // do other code here.
    }
}
like image 30
thomasmalt Avatar answered Sep 21 '22 13:09

thomasmalt