Reputation: 1209
I'm using HHVM to write a system tool and I cannot for the life of me figure out why this code issues an error when I run 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;
}
}
This issues the following when I run hh_client
/usr/lib/mango/tools/autoloader.hh:9:9,19: Expected
The line it is pointing to is the line that says
global $__al_paths;
Which is being declared in the global
scope. This appears to be a syntax error, it is as if the global
keyword is not supported on HHVM, however I checked the documentation and it has several examples of it being using in Hack code.
Upvotes: 4
Views: 464
Reputation: 1209
UPDATE !!!
This seemed to solve the problem, I'd like to know why the global
keyword doesn't work though.
$__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;
}
}
Upvotes: 0
Reputation: 7260
First, HHVM itself supports everything from the PHP language when you're writing PHP code, including global
. This restriction is due to your usage of the Hack language -- global
is one of the things that we removed from the language. If you really really need globals, you can use $GLOBALS
in partial mode only, but strict mode disallows them altogether. (And if you see our documentation using global
, it's wrong, please use the button on the page to file a bug!)
Upvotes: 4
Reputation: 5889
Instead of using global
try to rewrite your code like this (called dependency injection):
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;
}
}
Then call it like:
$__al_paths = array();
requires('classpath', $__al_paths);
This way you produce much more flexible and more stable code than playing around with globals which should been deleted from every humans mind.
Upvotes: 1