Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:原子包含_Php_Function_Include - Fatal编程技术网

PHP:原子包含

PHP:原子包含,php,function,include,Php,Function,Include,我使用了很多include文件来包含191个包含函数、类等集合的文件 对我来说,一个问题是我真的不喜欢编辑include文件,它会变得一团糟,有时我会忘记包含一些东西 因此我想知道,是否有一个包含php或自制库的函数,它包含文件夹中的所有php文件,或者更好的是包含在它自己的文件夹+它的所有子文件夹中 这些东西使生活变得更加轻松和灵活。您可以使用我刚刚创建的以下功能: function load_folder($folder, $ext = '.php') { foreach (glob

我使用了很多include文件来包含191个包含函数、类等集合的文件

对我来说,一个问题是我真的不喜欢编辑include文件,它会变得一团糟,有时我会忘记包含一些东西

因此我想知道,是否有一个包含php或自制库的函数,它包含文件夹中的所有php文件,或者更好的是包含在它自己的文件夹+它的所有子文件夹中


这些东西使生活变得更加轻松和灵活。

您可以使用我刚刚创建的以下功能:

function load_folder($folder, $ext = '.php') {
    foreach (glob("$folder*$ext") as $file) { 
        if (file_exists($file)) {
            require_once($file);
        }
    }
}
开始编辑

这是同一函数的新版本。现在,它允许您将文件夹指定为
文件夹
文件夹
,而不会崩溃。现在它还会加载所有文件夹和子文件夹中的所有文件

function load_folder($dir, $ext = '.php') {
    if (substr($dir, -1) != '/') { $dir = "$dir/"; }
    if($dh = opendir($dir)) {
        $files = array();
        $inner_files = array();
        while($file = readdir($dh)) {
            if($file != "." and $file != ".." and $file[0] != '.') {
                if(is_dir($dir . $file)) {
                    $inner_files = load_folder($dir . $file);
                    if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
                } else {
                    array_push($files, $dir . $file);
                }
            }
        }
        closedir($dh);
        foreach ($files as $file) {
            if (is_file($file) and file_exists($file)) {
                $lenght = strlen($ext);
                if (substr($file, -$lenght) == $ext) { require_once($file); }
            }
        } 
    }
}
结束编辑

如果您只想加载文件夹中的
.txt
文件,您还可以指定一个特定的扩展名,例如:
load_folder('folder/','.txt')。
记住,有人认为这是不安全的。在商业网站中使用此功能之前,请查看有关此主题的更多意见。
还要注意,如果您的一些文件是关于类的,您可以使用
\uuu autoload()
PHP本机函数让PHP调用真正需要的类(延迟加载)

参考资料:


您可以使用我刚刚创建的以下功能:

function load_folder($folder, $ext = '.php') {
    foreach (glob("$folder*$ext") as $file) { 
        if (file_exists($file)) {
            require_once($file);
        }
    }
}
开始编辑

这是同一函数的新版本。现在,它允许您将文件夹指定为
文件夹
文件夹
,而不会崩溃。现在它还会加载所有文件夹和子文件夹中的所有文件

function load_folder($dir, $ext = '.php') {
    if (substr($dir, -1) != '/') { $dir = "$dir/"; }
    if($dh = opendir($dir)) {
        $files = array();
        $inner_files = array();
        while($file = readdir($dh)) {
            if($file != "." and $file != ".." and $file[0] != '.') {
                if(is_dir($dir . $file)) {
                    $inner_files = load_folder($dir . $file);
                    if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
                } else {
                    array_push($files, $dir . $file);
                }
            }
        }
        closedir($dh);
        foreach ($files as $file) {
            if (is_file($file) and file_exists($file)) {
                $lenght = strlen($ext);
                if (substr($file, -$lenght) == $ext) { require_once($file); }
            }
        } 
    }
}
结束编辑

如果您只想加载文件夹中的
.txt
文件,您还可以指定一个特定的扩展名,例如:
load_folder('folder/','.txt')。
记住,有人认为这是不安全的。在商业网站中使用此功能之前,请查看有关此主题的更多意见。
还要注意,如果您的一些文件是关于类的,您可以使用
\uuu autoload()
PHP本机函数让PHP调用真正需要的类(延迟加载)

参考资料:


如果类名和文件之间有标准,可以使用
\uu autoload()
函数。它将为您节省很多费用


如果类名和文件之间有标准,可以使用
\uu autoload()
函数。它将为您节省很多费用


您可以创建一个包含各个include语句的“meta include”文件,然后在脚本中只包含一个文件


当然,其他答案中的自动加载版本会更有效。虽然PHP在加载/解析方面非常快,但是每个请求加载191个单独的文件的速度会非常快。

您可以只拥有一个包含单独include语句的“meta include”文件,然后在脚本中只包含一个文件


当然,其他答案中的自动加载版本会更有效。虽然PHP在加载/解析方面非常快,但每个请求加载191个单独的文件将非常快。

我通常要求作为第一个应用程序\u top.PHP使用以下内容:

...

function requireAll($folder){
    // open the folder
    $libs = opendir($folder);
    // loop inside to include each file, excluding windows default 'meta-link' . and ..
    while ($lib = readdir($libs)) {
        if ($lib != "." && $lib != "..")
            // require_once to be sure to require only one time
            require_once $folder . $lib;
}
    // close the dir for cleaning stuff
closedir($libs);
}

//Require all helpers   
requireAll(DIR_HELPERS);

//Require all model classes 
requireAll(DIR_MODEL);

//Require all mappers   
requireAll(DIR_MAPPERS);

...

我通常需要将以下内容作为第一个应用程序_top.php:

...

function requireAll($folder){
    // open the folder
    $libs = opendir($folder);
    // loop inside to include each file, excluding windows default 'meta-link' . and ..
    while ($lib = readdir($libs)) {
        if ($lib != "." && $lib != "..")
            // require_once to be sure to require only one time
            require_once $folder . $lib;
}
    // close the dir for cleaning stuff
closedir($libs);
}

//Require all helpers   
requireAll(DIR_HELPERS);

//Require all model classes 
requireAll(DIR_MODEL);

//Require all mappers   
requireAll(DIR_MAPPERS);

...

不包含类的文件时:创建一个包含所有包含和包含该文件的文件。否则,当不包含类的文件时,请使用
\uuuu autoload()
:创建一个包含所有包含项的文件,并包含该文件。否则,使用
\uuu autoload()
实际上并不能解决将所有文件加载到文件夹中的问题。你是对的。它不能像你的剧本那样解决他的问题。无论如何,我认为一个php项目有很多类,并且知道uu autoload()函数可以保存很多requires/includes。它实际上并不能解决在一个文件夹中加载所有文件的问题。你是对的。它不能像你的剧本那样解决他的问题。无论如何,我认为php项目有很多类,并且知道uu autoload()函数可以节省很多requires/includes。非常感谢!load_folder是否也加载子文件夹,或者是否有其他功能???@matthy,如果你喜欢,我会处理,但你需要等待10或15分钟…谢谢,是的,如果你能帮助我,这将是我文件夹的真正救命稻草@马蒂,好了。查看我的编辑。我也测试过了,看起来效果不错。非常感谢!load_folder是否也加载子文件夹,或者是否有其他功能???@matthy,如果你喜欢,我会处理,但你需要等待10或15分钟…谢谢,是的,如果你能帮助我,这将是我文件夹的真正救命稻草@马蒂,好了。查看我的编辑。我也测试过它,它似乎工作得很好。