Reputation: 59
How to define variables in drupal module so that I can access those variables in that module from any function
Upvotes: 3
Views: 2886
Reputation: 27043
Try variable_set() and variable_get()
Set the variable like that:
// I have assumed the variable name to be "the_name_of_the_variable"
variable_set("the_name_of_the_variable", "the value of the variable");
And then retrieve the value like that:
$my_variable = variable_get("the_name_of_the_variable", "a default value in case the variable has never been set before");
Upvotes: 5
Reputation: 2458
For every page load in Drupal all the .module
files are loaded. But this is not the case for the inc
files.
Inc files are loaded only when it is mentioned in the menu hook or they are explicitly mentioned in module file using module_load_include
// Load node.admin.inc from the node module.
module_load_include('inc', 'node', 'node.admin');
I think this pretty much answers your question. So you should define your variables as global in these files.
By your question if you mean that the scope of the functions defines in your module also should be limited to your module only, then I don't think there is any solution as of now. But at-least the above method will make sure that your variables are not loaded, when your module is not performing any actions.
Upvotes: 0