一次包含子文件夹的php文件



在PHP中,我有一个包含子文件夹的文件夹,其中包含带有类或接口的.PHP文件。该级别以下没有其他子文件夹。某些类扩展了在其他文件夹中指定的其他类

结构:

MAINFOLDER
- FOLDER1
-- Class1.php
-- Interface1.php
-- Class2.php
- FOLDER2
-- Class3.php
-- Class4.php
-- Interface2.php
- FOLDER3
-- Interface3.php
-- Class5.php

我试过这个代码:

$foldernames = ['FOLDER1','FOLDER2','FOLDER3'];
foreach ($foldernames as $foldername) {
foreach (glob('MAINFOLDER/'.$foldername.'/*.php') as $file) {
include_once $file;
}
}

代码不起作用。有人知道如何轻松地同时包含所有类和接口吗?

spl_autoload_register((就是为此而生的。

它将使用参数中给定的回调来自动包含一个丢失的类。

例如:

spl_autoload_register(function ($className)
{
$foldernames = ['FOLDER1','FOLDER2','FOLDER3'];
foreach ($foldernames as $foldername)
{
//This example requires, of course, a correct naming of class files
$file = 'MAINFOLDER/' . $foldername . '/' . $className . '.php';
if (file_exists($file))
include $file;
}
});

最新更新