【问题标题】:View PHP file with includes included查看包含包含的 PHP 文件
【发布时间】:2014-10-14 19:44:42
【问题描述】:

在“预处理器”通过它并包含所有“include”和“require”文件之后,是否有一个工具可以显示我的 .php 文件的样子?换句话说,如果我有一个名为“index.php”的文件:

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

如果文件是: vars.php:

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

header.php:

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

body.php:

<?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 的同事,他的反应是“这真的很有用,但我从未见过任何可以证明这一点的东西。”

【问题讨论】:

  • 我使用的工具是我的大脑和一个好的 IDE
  • 如果您正确地包含所有文件,按照您希望它编译的顺序,它将按照您描述的顺序加载。如果您有错误,它会告诉您组件丢失或未正确包含或您有什么。
  • @Dagon 那,如果我的大脑出现故障,我会切换到 XDebug ;)
  • @PetervanderWal 你需要 Brain 2.0(有 7 种颜色)
  • @Dagon 我还在测试版中:(

标签: php php-include


【解决方案1】:

如果您愿意,您可以创建自己的工具。只需使用 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 或 required_once 文件(请参阅here)。唯一的限制是您只能看到文本或 HTML 输出,因此 vars.php 不会有可见内容。如果你更改一个纯 php 文件,给它一个 .phps 的扩展名,那么你可以看到 PHP 源代码会突出显示。

【讨论】:

    猜你喜欢
    • 2012-02-13
    • 2011-08-02
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多