Php 自动包含/要求所有目录下的所有文件

Php 自动包含/要求所有目录下的所有文件,php,autoloader,Php,Autoloader,我想自动在所有目录下包含/要求所有.php文件。例如: (目录结构) 我需要一个函数,可以自动加载以.php 我试过各种方法: $scan = scandir('classes/'); foreach ($scan as $class) { if (strpos($class, '.class.php') !== false) { include($scan . $class); } } 如果要包含的php文件是php类,那么应该使用 自动在所有目录中包含所有p

我想自动
在所有目录下包含/要求
所有
.php
文件。例如:

(目录结构)

我需要一个函数,可以自动加载以
.php

我试过各种方法:

$scan = scandir('classes/');
foreach ($scan as $class) {
    if (strpos($class, '.class.php') !== false) {
        include($scan . $class);
    }
}

如果要包含的php文件是php类,那么应该使用

自动在所有目录中包含所有php文件不是安全的做法。如果包含不必要的文件,性能可能会降低

下面是应该可以工作的代码(我还没有测试过):


如果你想要递归包含

好吧,如果你想在子目录中包含所有以某个结尾的php文件,你必须创建一个递归函数。大概是这样的:

function load_classphp($directory) {
    if(is_dir($directory)) {
        $scan = scandir($directory);
        unset($scan[0], $scan[1]); //unset . and ..
        foreach($scan as $file) {
            if(is_dir($directory."/".$file)) {
                load_classphp($directory."/".$file);
            } else {
                if(strpos($file, '.class.php') !== false) {
                    include_once($directory."/".$file);
                }
            }
        }
    }
}

load_classphp('./classes');

这可能是递归查找模式文件的最简单方法:

$dir = new RecursiveDirectoryIterator('classes/');
$iter = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($iter, '/^.+\.class\.php$/', RecursiveRegexIterator::GET_MATCH); // an Iterator, not an array

foreach ( $files as $file ) {
  include $file; // $file includes `classes/`
}
RecursiveDirectoryIterator
是一个棘手的问题,因此了解它的所有内容可能是不可行的。不过,interwebs有很多具体的例子。诀窍在于寻找它

$dir = new RecursiveDirectoryIterator('change this to your custom root directory'); foreach (new RecursiveIteratorIterator($dir) as $file) { if (!is_dir($file)) { if( fnmatch('*.php', $file) ) // you can change the file extension to any that you require. /* do anything here */ } } $dir=new recursivedirectoryinterator('change this to your custom root directory'); foreach(新的递归迭代器($dir)作为$file){ 如果(!is_dir($file)){ if(fnmatch('*.php',$file))//您可以将文件扩展名更改为所需的任何扩展名。 /*在这里做什么*/ } }
一种简单的方法:使用
recursivedirectoryinterator
而不是
glob()
的简单函数


为什么不起作用?上面说什么?确保
error\u reporting
display\u errors
处于启用状态。它只给我未定义的变量,表示它没有加载类。替换
include($scan.$class)的code>包括('classes/'。$class)OMG的
unset()
。。。如果您不想要
,请不要索要它们。了解scandir()的功能,以及它的竞争对手,比如glob()
我可以看到它的功能。现在是6点。但这仍然很糟糕,有些函数可以满足您的需求
glob()
不带模式,返回除
之外的所有文件和文件夹。
。还有更多的函数可以完全满足您的需要。谢谢您,这段代码片段完成了几个小时来修补glob和autoload所没有完成的工作。@lindsaymacvean glob也可以工作,同样可以使用递归函数(像这样-您可以循环结果)(但也可以修改函数以直接包含文件)。
$dir = new RecursiveDirectoryIterator('classes/');
$iter = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($iter, '/^.+\.class\.php$/', RecursiveRegexIterator::GET_MATCH); // an Iterator, not an array

foreach ( $files as $file ) {
  include $file; // $file includes `classes/`
}
$dir = new RecursiveDirectoryIterator('change this to your custom root directory'); foreach (new RecursiveIteratorIterator($dir) as $file) { if (!is_dir($file)) { if( fnmatch('*.php', $file) ) // you can change the file extension to any that you require. /* do anything here */ } }
function autoload( $path ) {
    $items = glob( $path . DIRECTORY_SEPARATOR . "*" );

    foreach( $items as $item ) {
        $isPhp = pathinfo( $item )["extension"] === "php";

        if ( is_file( $item ) && $isPhp ) {
            require_once $item;
        } elseif ( is_dir( $item ) ) {
            autoload( $item );
        }
    }
}

autoload( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . "classes" );
function include_dir_r( $dir_path ) {
    $path = realpath( $dir_path );
    $objects = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $path ), \RecursiveIteratorIterator::SELF_FIRST );
    foreach( $objects as $name => $object ) {
        if( $object->getFilename() !== "." && $object->getFilename() !== ".." ) {
            if( !is_dir( $name ) ){
                include_once $name;
            }
        }
    }
}