包括PHP或Wildcard中的整个目录,以便在PHP Include中使用? [英] Including a whole directory in PHP or Wildcard for use in PHP Include?

查看:191
本文介绍了包括PHP或Wildcard中的整个目录,以便在PHP Include中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php中有一个命令解释器。它位于命令目录中,需要访问命令文件中的每个命令。目前我在每个命令上调用一次。

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

是否有一些必须包含所有这些命令和一个require_once?我的代码中有许多其他地方(包括工厂,建设者和其他口译员)也有类似的问题。此目录中只有命令,解释器需要目录中的所有其他文件。是否有可以在require中使用的通配符?例如:

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?

推荐答案

为什么要这样做?当需要它来增加速度和减少占用空间时,这不是一个更好的解决方案吗?

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?

这样的事情:

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 whatever else you want here.
    }
}

这篇关于包括PHP或Wildcard中的整个目录,以便在PHP Include中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆