5

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?

4
  • you can use different names for the global variables inside each module, and can use it differently from the respective module. Even if it is visible to other modules, if it is not called it won't affect anything.
    – AjitS
    Commented Sep 17, 2012 at 14:09
  • Yeah, I could do that. But in this case I'll have to change name in all the places, where it is used. But the trick was to use the same variable name to change the code as little as possible.
    – user4035
    Commented Sep 17, 2012 at 14:39
  • If Global variable holds FILE name why not use FILE instead every where ? Whats the need to use GLOBAL var ? Or you can define CONSTANTS inside each file that has the actual Table name so in future if you decide to rename table/file it will be of less trouble.
    – GoodSp33d
    Commented Sep 17, 2012 at 14:53
  • I didn't know about FILE variable. It can be used for this task. Can you give an example of defining CONSTANTS with the same name, but different values in multiple files, so the value will be visible only inside the appropriate file?
    – user4035
    Commented Sep 17, 2012 at 15:26

2 Answers 2

4

If you use the same global variable, its value is shared between all the enabled modules because global variables are globals by definition. Every PHP file that is loaded will see the same global variables, such as the global $user that is used from Drupal for the account of the currently logged-in user.

The same is true if you use constants defined with define(); they are global, and two modules cannot define the same constants. Try defining the REQUEST_TIME in Drupal 7, with define('REQUEST_TIME, 'test'): You will get an error, as the same constant is already defined from Drupal (see REQUEST_TIME).

The same is true for Drupal variables defined with variable_set(). Every module that is using variable_set($name, $value) (where $name has the same value for all the modules) is setting the same variable.

Even if you would be using &drupal_static($name, $value) in different modules, the modules will be altering the same static variable.

I would rather use a constant defined with define(), but every module needs to use a different constant.
See the Drupal coding standards about naming conventions.

0

why you dont use variable ? variables is good feature in drupal and working wth them is safe and easy.

variable_set('module_name',FILE); variable_set Doc

and those variable provide everywhere you want just need get them

variable_get($name, $default = NULL);

variable_get Doc

I suggest you use variabled instead global variables.

1
  • Because in principle, there is no difference. If I set the value to the same variable in different modules, it's value will be overwritten by every module. And I wanted to use many variables with the same name, that will be visible only inside the appropriate module.
    – user4035
    Commented Sep 18, 2012 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.