Turning all variables into global variables

0

I'm developing a project and it works with many files and these files are called via require_once() .

It is hard work to declare each variable created as a global variable in view of the number of files, obviously it is not exactly all of them, but the ones I need to declare need attention, because sometimes I forget and the variable error is undefined. / p>

I did a test here and got the native function of PHP get_defined_vars() , turning all variables into global variables automatically. The code for this is as follows:

foreach (get_defined_vars() as $name_var => $content_var)
{
    $GLOBALS[$name_var] = $content_var;
}

It made the process easier, because I now get a return of all system variables with a simple var_dump($GLOBALS) .

But even this working I consider a gambiarra, because this method can generate problems with the variables since the variables of the same name will be replaced.

I also worry about the information overload since the way I executed the function all variables, without exception, will be a global one, so even the variables that are no longer useful in processing will be transformed into global.

  

Is there any way to improve this process by doing the same thing I want to do in a more viable way?

I thought about using a class that would be in charge of this process, where I could set the variables I want to have global access. That way I centralize all conversions.

    
asked by anonymous 05.03.2018 / 05:35

0 answers