I have several modules, that implement very close functionality. And the SQL requests are almost the same, except for the table name. So I wanted to create a global variable, inside each module:
$GLOBALS['module_name'] = __FILE__;
$regexp = "/.*" . "\\" . DIRECTORY_SEPARATOR. '(.*)\.module/';
$GLOBALS['module_name'] = preg_replace($regexp, '$1', $GLOBALS['module_name']);
$GLOBALS['module_name'] will contain the current file name, which is the same as name of the table.
And then use it in the requests:
$query = db_select($GLOBALS['module_name']);
But global variables are shared among the modules, and the definition from a different module rewrites the correct value.
I tried to use hook_init
function my_module_init()
{
$GLOBALS['module_name'] = __FILE__;
$regexp = "/.*" . "\\" . DIRECTORY_SEPARATOR. '(.*)\.module/';
$GLOBALS['module_name'] = preg_replace($regexp, '$1', $GLOBALS['module_name']);
}
But without success.
Is it possible to have global variables with the same name in many modules, but each will be visible only inside the appropriate module?