2

Is there a tool that will show me what my .php file looks like AFTER the "preprocessor" goes through it and includes all the "include" and "require" files? In other words, if I have a file called "index.php":

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

If the files are: 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";
?>

I would like to be able to see that this page results in a working page that looks like:

<?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";
?>

This is obviously a contrived example, but I am working with a site with includes that are nested about 4 deep, and I would like to make sure that what PHP is working with is really what I want it to be working with.

I am fairly new to PHP, but I asked this question of a colleague who is also working with PHP and his reaction was "That would be really usedful, but I have never seen anything that will show that."

MillerTed
  • 21
  • 2
  • 2
    THe tool i use for this is my brain and a good IDE –  Oct 14 '14 at 19:47
  • If you include all your files properly, in the order you want it to compile, it will load in the order you are describing. If you have errors on, it will tell you that a component is missing or not included properly or what-have-you. – Rasclatt Oct 14 '14 at 19:49
  • @Dagon That, and if my brain is failing I switch to XDebug ;) – Peter van der Wal Oct 14 '14 at 19:49
  • These are sad news, but if such a problem occur there is usually some kind of problem with the application logic. All includes should be only libraries, while the render engine of an application should be written in clear and not "multinested" way. This is only a comment of course, and my own opinion. I can be wrong. – Jacek Kowalewski Oct 14 '14 at 19:49
  • 3
    @PetervanderWal you need Brain 2.0 (comes in 7 colours) –  Oct 14 '14 at 19:50
  • Given that includes can be dynamic, or dependent on runtime conditions, it wouldn't really be practical to build such a tool; and would be a lot less useful than you imagine – Mark Baker Oct 14 '14 at 19:50
  • I think it's perhaps time for you to switch to a PHP Framework with MVC pattern. – Alban Pommeret Oct 14 '14 at 19:53
  • 1
    @Dagon I'm still on the Beta-release :( – Peter van der Wal Oct 14 '14 at 19:55

1 Answers1

0

If you wish, you may create your own tool. Just use get_included_files and file_get_contents, as follows:

<?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 );
}
?>

According to the manual, get_included_files() will get any required or required_once files, too (see here). The only limitation is that you'll only be able to see text or HTML output, so vars.php wouldn't have visible content. If you change a pure php file by giving it an extension of .phps, then you can see the PHP source code which can appear highlighted.

slevy1
  • 3,797
  • 2
  • 27
  • 33