How to change PHPs behavior to echo an HTML comment on every include() and require()
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.
<?php echo "<p>Hey everyone!</p>"; ?>
<?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.
You don't want that.
I'm serious, let's say you have
setup.phptries 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:
Now, if you want to mark a
Not really possible. You cannot replace/override internal PHP functions with new implementations. You COULD create your own wrappers, e.g.
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.
You may consider using the following:
To make sure it works properly, you should have a
ob_get_clean()called at the end of the file