0

In my current company we don't use MVC so it's pretty chaotic and I often struggle on where to find the PHP files which are responsible for specific things that I see on frontend. I was thinking on how to solve this chaos, I want to effectively know where to find those files immediately when I see the page in my browser. (We edit code directly on production server via FTP, without any testing or development environment so we are basically unable to search the code)

My idea is to achieve this:

Every include(), include_once(), require(), require_once() will echo an HTML comment of the PHP file which is being included, and after the file ends, it will do another "closing" HTML comment.

Example:

another_file.php

<?php echo "<p>Hey everyone!</p>"; ?>

Index.php:

<?php 
echo "<p>Hello World</p>";
include('another_file.php');
?>

What the browser renders:

<p>Hello World</p>
<!-- 'another_file.php' -->
<p>Hey everyone!</p>
<!-- END 'another_file.php' -->

Is there a way I can implement this? Maybe some PHP extension, or settings? This would be real time and nerve saver. Thank you.

clami219
  • 2,958
  • 1
  • 31
  • 45

3 Answers3

5

You don't want that.

I'm serious, let's say you have require("setup.php"); and setup.php tries to do session_start()... uh-oh, output has already been sent (the comment) so you can't set headers!

As Luc says, you can write your own function to do this. It can be quite simple:

function marked_require($file) {
    echo "<!-- '".$file."' -->\n";
    require($file);
    echo "<!-- END '".$file."' -->\n";
}

Now, if you want to mark a require, use marked_require instead!

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

Not really possible. You cannot replace/override internal PHP functions with new implementations. You COULD create your own wrappers, e.g.

function my_include($file) {
    echo "opening $file";
    include($file);
    echo "$file has been included";
}

but then you'd have to re-write ALL of your scripts to call these new wrappers.

And of course, this opens up a whole boatload of other issues, such as any config files which define global variables will now have those variable defined in the scope of your wrapper function. e.g. as soon as your my_include() finishes, those config variables will be garbage collected and gone.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

You may consider using the following:

function my_include($source)
{
    ob_start(); //Avoids printing to screen to allow header editing
    echo "<!-- '$source' -->\n";
    include($source);
    echo "<!-- END '$source' -->\n";
    if(strlen(ob_get_content())>0) //If there was some output, outputs everything
        ob_get_clean();
}

To make sure it works properly, you should have a ob_get_clean() called at the end of the file

clami219
  • 2,958
  • 1
  • 31
  • 45
  • Thank you, this is interesting code you provided, but sadly, they say that you cannot override require, include, require_once, include_once functions :( – Alexander Skiller Jul 31 '14 at 15:53
  • @AlexanderSkiller You are right, it seems that `include` and `require` are not common functions, but "Control structures", according to http://stackoverflow.com/questions/12743159/php-override-function-doesnt-override-even-if-bool-true-is-returned ... I fix my answer – clami219 Jul 31 '14 at 16:00