How do you want to define your module variables in drupal 6?

I'm in my modular file. I want to define some complex variables for use throughout the module. For simple things, I do this:

function mymodule_init() { define('SOME_CONSTANT', 'foo bar'); } 

But this will not work for more complex structures. Here are some ideas that I thought of:

global:

 function mymodule_init() { $GLOBALS['mymodule_var'] = array('foo' => 'bar'); } 

variable_set:

 function mymodule_init() { variable_set('mymodule_var', array('foo' => 'bar')); } 

module class property:

 class MyModule { static $var = array('foo' => 'bar'); } 

Variable_set / _get seems to be the most "drupal" way, but I'm drawn to setting up the class. Are there any flaws? Are there any other approaches?

+4
source share
3 answers

I have not seen anyone save static values ​​that are array objects.

For simple values, the drupal path is to put define at the beginning of the .module file. This file is loaded when the module is activated, so this is enough. It makes no sense to embed it in the hook_init function.

variable_set stores the value in the database, so do not run it again and again. Instead, you can put it in your hook_install to define them once. variable_set is useful if the value can be changed in the administrator section, but this is not the best choice for storing a static variable, since you will need a query to retrieve it.

+3
source

I think all of these methods will work. I never used it that way, but I believe that the context module ( http://drupal.org/project/context ) also has its own API for storing variables in a static cache. You can check the documentation for the module .

0
source

It is always useful to avoid global bindings. So your choice will be a little easier.

If you are mistaken about a class, you will write code that does not comply with D6 standards. There are many modules that do this, but I personally like to stay close to the core of Drupal so I can understand it better. And code written in different styles using the same application can adversely affect performance and maintenance.

variable_set () and define () are completely different. Use the first when you can expect information to change (variable). Use the latter for constants. Your requirements should be clear how to use.

Do not worry about getting into the database for the set_get variable. If your software is well written, it should not greatly affect performance. The performance of such work should be realized only if your applications have serious performance problems and you have tried everything else.

0
source

Source: https://habr.com/ru/post/1306287/


All Articles