php包含所有文件,如何包含()目录中的所有PHP文件?

回答(12)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

foreach (glob("classes/*.php") as $filename)

{

include $filename;

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

这是我在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;

}

}

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我意识到这是一个较旧的帖子但是......不要包括你的课程......而是使用__autoload

function __autoload($class_name) {

require_once('classes/'.$class_name.'.class.php');

}

$user = new User();

然后每当你调用一个尚未包含的新类时,php将自动触发__autoload并为你包含它

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

这只是对Karsten代码的修改

function include_all_php($folder){

foreach (glob("{$folder}/*.php") as $filename)

{

include $filename;

}

}

include_all_php("my_classes");

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

如果您使用的是PHP 5,则可能需要使用autoload .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

How to do this in 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

} );

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

如果要将all包含在目录及其子目录中:

$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;

}

不要忘记它将使用字母顺序来包含您的文件 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

如果文件之间有 NO dependencies ...这里是一个递归函数include_once所有子目录中的所有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 "\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');

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我建议你使用readdir()函数,然后循环并包含文件(参见该页面上的第一个例子) .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

如果你想要包含一堆类而不必一次定义每个类,你可以使用:

$directories = array(

'system/',

'system/db/',

'system/common/'

);

foreach ($directories as $directory) {

foreach(glob($directory . "*.php") as $class) {

include_once $class;

}

}

这样你就可以在包含类的php文件中定义类而不是 $thisclass = new thisclass(); 的整个列表

至于它处理所有文件的程度如何?我不确定这可能会有轻微的速度降低 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

不写一个函数()来包含目录中的文件 . 您可能会丢失变量范围,并且可能必须使用“全局” . 只需循环文件即可 .

此外,当包含的文件具有将扩展到另一个文件中定义的另一个类的类名时,您可能会遇到困难 - 这个类尚未包含在内 . 所以,要小心 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值