3

如何在 drupal 模块中定义变量,以便我可以从任何函数访问该模块中的这些变量

4

2 回答 2

5

试试variable_set () 和variable_get ()

像这样设置变量:

// 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");

然后像这样检索值:

$my_variable = variable_get("the_name_of_the_variable", "a default value in case the variable has never been set before");
于 2012-11-23T12:46:12.783 回答
0

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.

于 2012-11-23T14:47:07.377 回答