Subscribe to RSS - namign conventions

namign conventions

Tip for Managing Variables in a Drupal Module

Update via Garrett Albright (see comments)

Just a quick tip for managing variables in a Drupal module. Please prefix your variables with your module name. For example:

  1. variable_set('examplemodule_variable_name', 1234);

This ensures that another module won't override your variable, or your module won't do the same thing. Pretty simple. But what is also nice, is that you can clean up after your module a lot easier in the uninstall hook:

  1. /**
  2.  * Implementation of hook_uninstall().
  3.  */
  4. function examplemodule_uninstall() {
  5. // Get global variable array
  6. global $conf;
  7. foreach (array_keys($conf) as $key) {
  8. // Find variables that have the module prefix
  9. if (strpos($key, 'examplemodule_') === 0) {
  10. variable_del($key);
  11. }
  12. }
  13. }

Please note that the above convention could cause problems if your module name is something like content_permissions and then the content module (CCK) defines a variable with a conflicting name. All the more reason to make your module names more unique and without underscores.