How to include() all PHP files from a directory?

2019-01-03 20:07发布

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

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.

标签: php include
12条回答
干净又极端
2楼-- · 2019-01-03 20:39

Here is the way I include lots of classes from several folders in PHP 5. This will only work if you have classes though.

/*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楼-- · 2019-01-03 20:44

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

$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');
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-03 20:52

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

查看更多
Deceive 欺骗
5楼-- · 2019-01-03 20:53

I realize this is an older post BUT... DON'T INCLUDE YOUR CLASSES... instead use __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

查看更多
地球回转人心会变
6楼-- · 2019-01-03 20:53

You can use set_include_path:

set_include_path('classes/');

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

查看更多
虎瘦雄心在
7楼-- · 2019-01-03 20:53

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

As for how well it handles all the files? I'm not sure there might be a slight speed decrease with this.

查看更多
登录 后发表回答