查看包含includes的PHP文件


View PHP file with includes included

有没有一个工具可以在"预处理器"完成并包括所有"include"answers"require"文件后显示我的.php文件的样子?换句话说,如果我有一个名为"index.php"的文件:

<?php
#My root index page
include 'vars.php';
include 'header.php';
include 'body.php';
?>

如果文件是:vars.hp:

<?php
SITENAME = "MySite";
$where =  "here";
include 'ThatOne.php';
?>

header.php:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title>My page</title>
</head>

body.hp:

<?php
print "<body>'n";
print "<H1>Hello World</H1>'n";
print "</body>'n";
?>

ThatOne.php

<?php
$ThatOne =  "This One";
?>

我希望能够看到这个页面产生一个工作页面,看起来像:

<?php
#My root index page
SITENAME = "MySite";
$where =  "here";
$ThatOne =  "This One";
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title>My page</title>
</head>
<?php
print "<body>'n";
print "<H1>Hello World</H1>'n";
print "</body>'n";
?>

这显然是一个人为的例子,但我正在处理一个包含嵌套深度约为4的网站,我想确保PHP所使用的是我希望它真正使用的内容。

我对PHP还很陌生,但我问了一位同样使用PHP的同事这个问题,他的反应是"这真的很有用,但我从来没有见过任何能证明这一点的东西。"

如果您愿意,您可以创建自己的工具。只需使用get_included_files和file_get_contents,如下所示:

<?php
#My root index page
include 'vars.php';
include 'header.php';
include 'body.php';
$included_files = get_included_files();
foreach ($included_files as $filename) {
    echo file_get_contents( $filename );
}
?>

根据手册,get_included_files()也将获得任何必需或required_once文件(请参阅此处)。唯一的限制是您只能看到文本或HTML输出,因此vars.php不会有可见的内容。如果您通过给纯php文件扩展名.phps来更改它,那么您可以看到突出显示的php源代码。