tags:

views:

105

answers:

3

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

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

But that won't work for more complex structures. Here are some ideas that I've thought of:

global:

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

variable_set:

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

property of a module class:

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

Variable_set/_get seems like the most "drupal" way, but I'm drawn toward the class setup. Are there any drawbacks to that? Any other approaches out there?

A: 

I think all of those methods would work. I have never used it in this fashion, but I believe the context module (http://drupal.org/project/context) also has its own API for storing variables in a static cache. You may want to check out the documentation for the module.

bkildow
A: 

I haven't seen any one storing static values that are array objects.

For simple values the drupal way is to put a define in the begining of a modules .module file. This file is loaded when the module is activated so that is enough. No point in putting it in the hook_init function.

variable_set stores the value in the database so don't run it over and over. Instead you could put it in your hook_install to define them once. variable_set is good to use if the value can be changed in an admin section but it's not the best choice to store a static variable since you will need a query to fetch it.

googletorp
A: 

It's always a good practice to avoid globals. So that makes your choice a bit easier.

If you err towards a class, you'll be writing code that is not consistent with the D6 standards. There are a lot of modules that do it but I personally like to keep close to the Drupal core so I can understand it better. And code that's written in different styles through the same application can have an adverse effect on productivity and maintenance.

variable_set() and define() are quite different. Use the former when you can expect that information to change (a variable). Use the latter for constants. Your requirements should be clear as which one to use.

Don't worry too much about hitting the database for variable_set/get. If your software is written well, it should not effect performance hardly at all. Performance work arounds like that should only be implemented if your applications has serious performance issues and you've tried everything else.

Rimian
define can't be used for anything more complex than a scalar variable. Hence the question....
sprugman
As for using two different styles, if my module is defining its own little world within the broader cms, I just don't see it as a problem. Obviously all the hook_ functions are going to be following the drupal conventions, and I'll use drupal's convenience functions, such as l(), etc.
sprugman

related questions