0
I’m migrating from PHP 5 to 7 and one of the changes was that global variables stopped existing. And I have two variables of this style in my code:
$root = $GLOBALS['files']['siteRoot'];
$root = str_replace("//", "/", $root);
$pastas[] = "";
$pastas[] = "/controle/";
$pastas[] = "/pagseguro/";
$pastas[] = "/controle/auxiliar/";
$pastas[] = "/persistencia/";
$pastas[] = "/persistencia/DTO/";
$pastas[] = "/view/";
$pastas[] = "/view/classes/";
$pastas[] = "/view/relatorios/";
$pastas[] = "/library/";
$pastas[] = "/library/zip/";
$pastas[] = "/library/phpmailer/";
spl_autoload_register($nomeDaClasse) {
globals $root, $pastas;
$file = "";
for ($index = 0; $index < count($pastas); $index++) {
if (file_exists($root . $pastas[$index] . $nomeDaClasse . '.php')) {
$file = $root . $pastas[$index] . $nomeDaClasse . '.php';
}
}
if ($file == "") {
return false;
}
include $file;
}
I was recommended to use Static but I don’t know how to declare them.
Static variables and global variables are conceptually distinct and one does not replace the other. You use the structure
globals
to import into the current context something that has been defined in an external context. You have more elegant ways of doing the same, such as passing values by parameters if it is a function. Could [Dit] the question and present the full scope of where you are using such variables?– Woss
That’s the whole code
– Gabriel Soares