Doubt attribute declaration in PHP 7

Asked

Viewed 53 times

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?

  • That’s the whole code

1 answer

1


First, do:

spl_autoload_register($nomeDaClasse) {
    ...
}

It’s wrong and it doesn’t make any sense. The function spl_autoload_register is native to PHP and aims to record functions for the autoload. In the above section you are neither calling the function, nor setting it again. That is, the syntax is completely wrong. If you have difficulty visualizing this, I strongly recommend that you review the part that shows how to call a function and how to define it.

What you should do is call the function and set another to be registered:

spl_autoload_register(function ($nomeDaClasse) {
    ...
});

Note that the code is now spl_autoload_register(X), where X is an anonymous function. This anonymous function will be autoload. You can also do it if you prefer:

function minha_autoload($nomeDaClasse) {
    ...
}

spl_autoload_register('minha_autoload');

The result will be the same as using the anonymous function.

Ok, but how to pass values to function scope? If it is an anonymous function, you can use the use which serves precisely for this:

$root = 1;
$pastas = 2;

spl_autoload_register(function ($nomeDaClasse) use ($root, $pastas) {
    // $root e $pastas existirão aqui com os mesmos valores
    ...
});

Where $root and $pastas are variables defined in the same scope as the anonymous function.

  • Thank you very much for your help, I will definitely review the function call section. It’s a new language for me and I’m still struggling a little to master it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.