PHP file includes an internal function, you need to save global variables. (Trying to wrap HTML comments)

In an attempt to speed up my workflow and help the guys with integration (I am a third-party developer). I am trying to expand a file by turning on a function by wrapping comments around each file to display its file name:

function include_module($path) {
    echo "\n\n<!-- MODULE: ".basename($path, '.php')." -->\n";
    include($path);
    echo "\n<!-- /MODULE: ".basename($path, '.php')." -->\n\n";
}   
include_module('form-controls.php');

However, this leads to loss of access to any variables set outside the function. I know that I can:

global $var

But this will only give me access to $ var (I know I can do $ var ['var1'], etc.), is there a way to do "global all" or can anyone think of another approach to wrap comments?

Greetings :)

+5
source share
2 answers

Try the following:

function include_module($path) {
    foreach($GLOBALS as $name => $value) global $$name;
    echo "\n\n<!-- MODULE: ".basename($path, '.php')." -->\n";
    include($path);
    echo "\n<!-- /MODULE: ".basename($path, '.php')." -->\n\n";
}   
include_module('form-controls.php');
+5
source

You can use the following to access global variables.

extract($GLOBALS, EXTR_REFS); 
+5
source

All Articles