PHP:atomatic包含 [英] PHP: atomatic include

查看:100
本文介绍了PHP:atomatic包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用很多包含文件来包含我的191个文件,其中包含函数集合类。



对我来说,一个问题是我真的不喜欢编辑包含文件,因为它包含了大量的内容,有时候我忘记了包含内容。



因此,我想知道,是否有包含php或自己制作的库的函数,它包含文件夹中的所有php文件,或者在其自己的文件夹及其所有子文件夹中更好。 b
$ b

这些东西让生活更轻松和灵活。 您可以使用以下功能我刚刚创建:

$ p $ 函数load_folder($文件夹,$ ext ='.php'){
foreach(glob ($ folder * $ ext)作为$ file){
if(file_exists($ file)){
require_once($ file);





START EDIT

这是相同功能的新版本。现在,它允许您将文件夹指定为文件夹文件夹/ ,而不会崩溃。

 函数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)和file_exists($ file)){
$ lenght = strlen($ ext);
if(substr($ file, - $ lenght)== $ ext){require_once($ file); }
}
}
}
}

END EDIT



如果您只想载入 .txt 文件如下所示: load_folder('folder /','.txt');
请记住,有人认为这在某种程度上是不安全的。在商业网站中使用此功能之前,请先查看关于该主题的更多意见。
还要注意,如果你的某些文件是关于类的,你可以使用 __ autoload() PHP原生函数让PHP调用真正需要的类



参考文献:


I am using a lot of include files to include my 191 files of with the collection of functions, classes ect.

A problem for me is I really dislike editing the include files its gets a big mess and sometime i just forget to include something.

Therefore I was wondering, is there a include function for php or own made library that includes all the php files in a folder or even better in its own folder + all its sub-folders.

These things make life much easyer and flexible.

解决方案

You can use the following function i just created:

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

START EDIT

This is the new version of the same function. Now it allows you to specify folders as folder or folder/ without crashing. Also now it loads all files in all folders and subfolders.

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); }
            }
        } 
    }
}

END EDIT

You can also specify a specific extension if you want to load for example only .txt files in a folder you can execute is like this: load_folder('folder/', '.txt');. Remember that someone think that this is somehow insecure. Before using this function inside a business site, look for more opinion about the topic. Notice also that if some of your files are regarding classes you could use the __autoload() PHP native function to let PHP call the class where it is really needed (lazy loading).

References:

这篇关于PHP:atomatic包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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