如何包含()目录中的所有 PHP 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/599670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include() all PHP files from a directory?
提问by occhiso
In PHP can I include a directory of scripts?
在 PHP 中,我可以包含一个脚本目录吗?
i.e. Instead of:
即而不是:
include('classes/Class1.php');
include('classes/Class2.php');
is there something like:
有没有类似的东西:
include('classes/*');
Couldn't seem to find a good way of including a collection of about 10 sub-classes for a particular class.
似乎找不到为特定类包含大约 10 个子类的集合的好方法。
回答by Karsten
foreach (glob("classes/*.php") as $filename)
{
include $filename;
}
回答by Marius
Here is the way I include lots of classes from several folders in PHP 5. This will only work if you have classes though.
这是我在 PHP 5 中从多个文件夹中包含大量类的方式。不过,这仅在您有类时才有效。
/*Directories that contain classes*/
$classesDir = array (
ROOT_DIR.'classes/',
ROOT_DIR.'firephp/',
ROOT_DIR.'includes/'
);
function __autoload($class_name) {
global $classesDir;
foreach ($classesDir as $directory) {
if (file_exists($directory . $class_name . '.php')) {
require_once ($directory . $class_name . '.php');
return;
}
}
}
回答by Banning
I realize this is an older post BUT... DON'T INCLUDE YOUR CLASSES... instead use __autoload
我意识到这是一个较旧的帖子,但是...不要包括您的课程...而是使用 __autoload
function __autoload($class_name) {
require_once('classes/'.$class_name.'.class.php');
}
$user = new User();
Then whenever you call a new class that hasn't been included yet php will auto fire __autoload and include it for you
然后,每当您调用尚未包含的新类时,php 都会自动触发 __autoload 并为您包含它
回答by foobar
this is just a modification of Karsten's code
这只是对 Karsten 代码的修改
function include_all_php($folder){
foreach (glob("{$folder}/*.php") as $filename)
{
include $filename;
}
}
include_all_php("my_classes");
回答by Sorin C
How to do this in 2017:
如何在 2017 年做到这一点:
spl_autoload_register( function ($class_name) {
$CLASSES_DIR = __DIR__ . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR; // or whatever your directory is
$file = $CLASSES_DIR . $class_name . '.php';
if( file_exists( $file ) ) include $file; // only include if file exists, otherwise we might enter some conflicts with other pieces of code which are also using the spl_autoload_register function
} );
Recommended by PHP documentation here: Autoloading classes
PHP 文档在这里推荐:自动加载类
回答by albanx
You can use set_include_path:
您可以使用set_include_path:
set_include_path('classes/');
回答by Scott Dawson
If your looking to include a bunch of classes without having to define each class at once you can use:
如果您希望包含一堆类而不必一次定义每个类,您可以使用:
$directories = array(
'system/',
'system/db/',
'system/common/'
);
foreach ($directories as $directory) {
foreach(glob($directory . "*.php") as $class) {
include_once $class;
}
}
This way you can just define the class on the php file containing the class and not a whole list of $thisclass = new thisclass();
这样你就可以在包含类的 php 文件上定义类,而不是整个列表 $thisclass = new thisclass();
As for how well it handles all the files? I'm not sure there might be a slight speed decrease with this.
至于它处理所有文件的情况如何?我不确定这可能会导致速度略有下降。
回答by Matthieu Chavigny
If you want include all in a directory AND its subdirectories:
如果要将所有内容都包含在目录及其子目录中:
$dir = "classes/";
$dh = opendir($dir);
$dir_list = array($dir);
while (false !== ($filename = readdir($dh))) {
if($filename!="."&&$filename!=".."&&is_dir($dir.$filename))
array_push($dir_list, $dir.$filename."/");
}
foreach ($dir_list as $dir) {
foreach (glob($dir."*.php") as $filename)
require_once $filename;
}
Don't forget that it will use alphabetic order to include your files.
不要忘记它会使用字母顺序来包含您的文件。
回答by Sergio Abreu
If there are NO dependenciesbetween files... here is a recursive function to include_once ALL php files in ALL subdirs:
如果文件之间没有依赖关系……这里是一个递归函数,用于在所有子目录中包含_once ALL php 文件:
$paths = array();
function include_recursive( $path, $debug=false){
foreach( glob( "$path/*") as $filename){
if( strpos( $filename, '.php') !== FALSE){
# php files:
include_once $filename;
if( $debug) echo "<!-- included: $filename -->\n";
} else { # dirs
$paths[] = $filename;
}
}
# Time to process the dirs:
for( $i=count($paths)-1; $i>0; $i--){
$path = $paths[$i];
unset( $paths[$i]);
include_recursive( $path);
}
}
include_recursive( "tree_to_include");
# or... to view debug in page source:
include_recursive( "tree_to_include", 'debug');

