How to change PHPs behavior to echo an HTML comment on every include() and require()

719

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.

660

Answer

Solution:

You don't want that.

I'm serious, let's say you haverequire("setup.php"); andsetup.php tries to dosession_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 arequire, usemarked_require instead!

11

Answer

Solution:

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 yourmy_include() finishes, those config variables will be garbage collected and gone.

785

Answer

Solution:

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 aob_get_clean() called at the end of the file

People are also looking for solutions to the problem: php - Can a trigger be used to alert me when a specific table is updated in MySQL?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.