PHP:列出所有的包含文件。

11

我有一个由许多PHP文件组成的大型复杂的项目。

是否有某个函数可以在我的代码中调用,返回所有已包含文件的列表?

3个回答

25

get_included_files 或者 get_required_filesget_included_files的别名)

http://us.php.net/manual/zh/function.get-included-files.php
http://us.php.net/manual/zh/function.get-required-files.phpget_included_files的别名)

<?php
// This file is abc.php

include 'test1.php';
include_once 'test2.php';
require 'test3.php';
require_once 'test4.php';

$included_files = get_included_files();

foreach ($included_files as $filename) {
    echo "$filename\n";
}
?>

-----
The above example will output:

abc.php
test1.php
test2.php
test3.php
test4.php

你可以直接使用 var_dump(get_included_files()) 而不是使用 foreach 循环。该函数返回一个数组。 - Casper Wilkes
这再次帮助了我,距我提出这个问题已经过去13年了。 - Liam

2
register_shutdown_function(
    function() {
        your_logger(get_included_files());
    }
);

get_included_files()函数将在脚本执行结束时被调用,因此您将获得所有已包含文件的完整列表。


虽然这可能是答案,但建议在回答中包含文档和/或解释。 - PurkkaKoodari
这是不正确的,该函数仅返回已包含的文件,并且顺序与函数调用时的顺序相同。 - Johannes


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接