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 开发者_如何学Gobig 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.
If you have a standard between the Class Names and the files you can use the __autoload()
function. It will save you a lot of includes.
http://php.net/manual/en/language.oop5.autoload.php
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:
- Autoloading classes
You could just have a 'meta-include' file which has the individual include statements, and then you only include that one single file in your scripts.
Of course, the auto-loading versions in the other answers here would be more efficient. While PHP's pretty fast at loading/parsing, 191 individual files to load for every request would add up pretty quick.
I usually require as first application_top.php with this:
...
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);
...
精彩评论