如何在drupal模块中定义变量,以便我可以从任何函数访问该模块中的那些变量
尝试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");
对于Drupal中的每个页面加载,所有.module
文件都被加载。但是inc
文件不是这种情况。
仅当菜单钩中提及Inc文件或使用module_load_include在模块文件中明确提及它们时才加载它们。>
// Load node.admin.inc from the node module. module_load_include('inc', 'node', 'node.admin');
我认为这几乎可以回答您的问题。因此,您应该在这些文件中将变量定义为全局变量。
[您的问题,如果您是指模块中定义的功能范围也应仅限于模块,那么我认为目前没有任何解决方案。但是,至少在模块未执行任何操作时,上述方法将确保不加载变量。