Hack typechecker does not recognize "global" keyword inside function

I use HHVM to write a system tool, and I can’t understand for life why this code throws an error on startup hh_client

$__al_paths = array();

function requires(string $classPath): void {
    global $__al_paths;
    $className = basename($classPath);
    if (!isset($__al_paths[$className])) {
       $__al_paths[$className] = AL_CLASSES_FOLDER.'/'.$classPath.'.'.AL_CLASS_EXTENSION;
    }
}

At startup hh_client

the following occurs:
/usr/lib/mango/tools/autoloader.hh:9:9,19: Expected

The line he points to is the line that says

global $__al_paths;

What is announced in the area global. This seems like a syntax error, since the keyword is globalnot supported in HHVM, however I checked the documentation and it has some examples of using this code in Hack code.

+4
source share
3 answers

-, HHVM , PHP, PHP-, global. Hack - global , . , $GLOBALS , . ( global, , , , !)

+4

global ( ):

function requires(string $classPath, $__al_paths): void {
    $className = basename($classPath);
    if (!isset($__al_paths[$className])) {
       $__al_paths[$className] = AL_CLASSES_FOLDER.'/'.$classPath.'.'.AL_CLASS_EXTENSION;
    }
}

:

$__al_paths = array();

requires('classpath', $__al_paths);

, , , .

+1

UPDATE !!!

This seemed to solve the problem; I would like to know why the keyword is globalnot working.

$__al_paths = array();

function requires(string $classPath): void {
    $__al_paths = $GLOBALS['__al_paths'];
    $className = basename($classPath);
    if (!isset($__al_paths[$className])) {
        $__al_paths[$className] = AL_CLASSES_FOLDER.'/'.$classPath.'.'.AL_CLASS_EXTENSION;
        $GLOBALS['__al_paths'] = $__al_paths;
    }
}
0
source

All Articles