[英]How to include() all PHP files from a directory?
在 PHP 中我可以包含一个脚本目录吗?
即代替:
include('classes/Class1.php');
include('classes/Class2.php');
有没有类似的东西:
include('classes/*');
似乎找不到为特定 class 包含大约 10 个子类的集合的好方法。
foreach (glob("classes/*.php") as $filename)
{
include $filename;
}
这是我在 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;
}
}
}
我意识到这是一篇较旧的帖子,但是...不要包括您的课程...而是使用 __autoload
function __autoload($class_name) {
require_once('classes/'.$class_name.'.class.php');
}
$user = new User();
然后,每当您调用尚未包含的新类时,php 都会自动触发 __autoload 并为您包含它
这只是对 Karsten 代码的修改
function include_all_php($folder){
foreach (glob("{$folder}/*.php") as $filename)
{
include $filename;
}
}
include_all_php("my_classes");
如何在 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
} );
PHP 文档在这里推荐:自动加载类
如果您使用的是PHP 5,则可能需要使用自动加载 。
您可以使用set_include_path :
set_include_path('classes/');
如果文件之间没有依赖关系……这里是一个递归函数,用于在所有子目录中包含_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');
<?php
//Loading all php files into of functions/ folder
$folder = "./functions/";
$files = glob($folder."*.php"); // return array files
foreach($files as $phpFile){
require_once("$phpFile");
}
如果您希望包含一堆类而不必一次定义每个类,您可以使用:
$directories = array(
'system/',
'system/db/',
'system/common/'
);
foreach ($directories as $directory) {
foreach(glob($directory . "*.php") as $class) {
include_once $class;
}
}
这样你就可以在包含类的 php 文件上定义类,而不是$thisclass = new thisclass();
的整个列表$thisclass = new thisclass();
至于它处理所有文件的情况如何? 我不确定这可能会导致速度略有下降。
如果要将所有内容都包含在目录及其子目录中:
$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;
}
不要忘记它会使用字母顺序来包含您的文件。
我建议您使用readdir()函数,然后循环并包含文件(请参阅该页面上的第一个示例)。
尝试为此目的使用库。
这是我构建的相同想法的简单实现。 它包括指定的目录和子目录文件。
包括全部
通过终端[cmd]安装
composer install php_modules/include-all
或者在 package.json 文件中将其设置为依赖项
{
"require": {
"php_modules/include-all": "^1.0.5"
}
}
使用
$includeAll = requires ('include-all');
$includeAll->includeAll ('./path/to/directory');
这是一个迟到的答案,它指的是 PHP > 7.2 到 PHP 8。
OP 没有在标题中询问类,但是从他的措辞我们可以看出他想要包含类。 (顺便说一句。这个方法也适用于命名空间)。
使用require_once可以用一条毛巾杀死三只蚊子。
spl_autoload_register( function ($class_name) {
require_once '/var/www/homepage/classes/' . $class_name . '.class.php';
} );
这将适用于类
new class_name;
或命名空间。 例如...
use homepage\classes\class_name;
从另一个问题移植过来的答案。 包括有关使用帮助程序 function 的限制的附加信息,以及用于加载包含文件中所有变量的帮助程序 function。
PHP 中没有原生的“include all from folder”。但是,实现起来并不复杂。 您可以 glob .php
文件的路径并将文件包含在循环中:
foreach (glob("test/*.php") as $file) {
include_once $file;
}
在这个答案中,我使用include_once
来包含文件。 请随时根据需要将其更改为include
、 require
或require_once
。
你可以把它变成一个简单的助手 function:
function import_folder(string $dirname) {
foreach (glob("{$dirname}/*.php") as $file) {
include_once $file;
}
}
如果您的文件定义了范围无关的类、函数、常量等,这将按预期工作。 但是,如果您的文件有变量,则必须使用get_defined_vars()
来“收集”它们并从 function 中返回它们。否则,它们将“丢失”到 function scope 中,而不是导入到原始的 scope 中。
如果您需要从 function 中包含的文件导入变量,您可以:
function load_vars(string $path): array {
include_once $path;
unset($path);
return get_defined_vars();
}
您可以将此 function 与import_folder
组合,将返回一个数组,其中包含包含文件中定义的所有变量。 如果你想从多个文件中加载变量,你可以:
function import_folder_vars(string $dirname): array {
$vars = [];
foreach (glob("{$dirname}/*.php") as $file) {
// If you want to combine them into one array:
$vars = array_merge($vars, load_vars($file));
// If you want to group them by file:
// $vars[$file] = load_vars($file);
}
return $vars;
}
以上将根据您的偏好(必要时注释/取消注释),将包含文件中定义的所有变量作为单个数组返回,或者按它们在其中定义的文件分组。
最后一点:如果您需要做的只是加载类,最好使用spl_autoload_register
按需自动加载它们。 使用自动加载器假定您已经构建了文件系统并一致地命名了类和名称空间。
不要编写 function() 来包含目录中的文件。 您可能会丢失变量范围,并且可能必须使用“全局”。 只需循环文件。
此外,当包含的文件的类名将扩展到另一个文件中定义的另一个类时,您可能会遇到困难 - 尚未包含。 所以,要小心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.