如何包含()目录中的所有PHP文件?

时间:2023-01-24 16:43:58

Very quick n00b question, in PHP can I include a directory of scripts.

非常快速的n00b问题,在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个子类。

12 个解决方案

#1


371  

foreach (glob("classes/*.php") as $filename)
{
    include $filename;
}

#2


50  

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

#3


26  

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并为您包含它

#4


19  

this is just a modification of Karsten's code

这只是对卡斯滕代码的修改

function include_all_php($folder){
    foreach (glob("{$folder}/*.php") as $filename)
    {
        include $filename;
    }
}

include_all_php("my_classes");

#5


18  

If you are using php 5 you might want to use autoload instead.

如果您使用的是php 5,那么您可能需要使用autoload。

#6


13  

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文档在这里推荐:自动读类

#7


9  

You can use set_include_path:

您可以使用set_include_path:

set_include_path('classes/');

http://php.net/manual/en/function.set-include-path.php

http://php.net/manual/en/function.set-include-path.php

#8


1  

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.

不要忘记它将使用字母顺序包括你的文件。

#9


1  

If there are NO dependencies between files... here is a recursive function to include_once ALL php files in ALL subdirs:

如果文件之间没有依赖关系……下面是一个递归函数,包含所有子目录中的所有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');

#10


0  

I suggest you use a readdir() function and then loop and include the files (see the 1st example on that page).

我建议您使用readdir()函数,然后循环并包含文件(请参阅该页面的第一个示例)。

#11


0  

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.

至于它如何处理所有的文件?我不确定会有轻微的减速。

#12


-2  

Do no write a function() to include files in a directory. You may lose the variable scopes, and may have to use "global". Just loop on the files.

不要编写一个函数()来包含目录中的文件。您可能会丢失变量作用域,并且可能必须使用“global”。只是对文件进行循环。

Also, you may run into difficulties when an included file has a class name that will extend to the other class defined in the other file - which is not yet included. So, be careful.

此外,如果包含的文件有一个类名,该类名将扩展到另一个文件中定义的另一个类(该类尚未包含),您可能会遇到困难。所以,要小心。

#1


371  

foreach (glob("classes/*.php") as $filename)
{
    include $filename;
}

#2


50  

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

#3


26  

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并为您包含它

#4


19  

this is just a modification of Karsten's code

这只是对卡斯滕代码的修改

function include_all_php($folder){
    foreach (glob("{$folder}/*.php") as $filename)
    {
        include $filename;
    }
}

include_all_php("my_classes");

#5


18  

If you are using php 5 you might want to use autoload instead.

如果您使用的是php 5,那么您可能需要使用autoload。

#6


13  

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文档在这里推荐:自动读类

#7


9  

You can use set_include_path:

您可以使用set_include_path:

set_include_path('classes/');

http://php.net/manual/en/function.set-include-path.php

http://php.net/manual/en/function.set-include-path.php

#8


1  

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.

不要忘记它将使用字母顺序包括你的文件。

#9


1  

If there are NO dependencies between files... here is a recursive function to include_once ALL php files in ALL subdirs:

如果文件之间没有依赖关系……下面是一个递归函数,包含所有子目录中的所有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');

#10


0  

I suggest you use a readdir() function and then loop and include the files (see the 1st example on that page).

我建议您使用readdir()函数,然后循环并包含文件(请参阅该页面的第一个示例)。

#11


0  

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.

至于它如何处理所有的文件?我不确定会有轻微的减速。

#12


-2  

Do no write a function() to include files in a directory. You may lose the variable scopes, and may have to use "global". Just loop on the files.

不要编写一个函数()来包含目录中的文件。您可能会丢失变量作用域,并且可能必须使用“global”。只是对文件进行循环。

Also, you may run into difficulties when an included file has a class name that will extend to the other class defined in the other file - which is not yet included. So, be careful.

此外,如果包含的文件有一个类名,该类名将扩展到另一个文件中定义的另一个类(该类尚未包含),您可能会遇到困难。所以,要小心。