[英]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.