Syndicate content

Topic “namign conventions”

By zzolo
2010, February 17 - 10:22am

Tip for Managing Variables in a Drupal Module : Naming conventions, please.

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.

Recent Books

Haiku and a Portrait

The Uncertainty Principle

Floating on the air
everywhere and somewhere,
nowhere, only here.

The Tweet of Zzolo

  • @atsmath It's just that secret! :)
    2 hours 31 min ago
  • Awesome! RT @eaton: Matt Mullenweg turns WP trademark over to the Nonprofit WordPress Foundation. Big news. Kudos! http://bit.ly/bopWpw
    4 hours 9 min ago
  • OpenLayers 2.10 out! Great work OL development team! http://trac.openlayers.org/wiki/Release/2.10 (JS library not Drupal module)
    8 hours 10 min ago
  • cool (ish). ESRI "gives away" ArcGIS to non-profit (tax-exempt), and open source its GeoPortal Ext http://bit.ly/aojcHZ http://bit.ly/cFQaR7
    8 hours 33 min ago
  • Office emaiI xchange: "I have electrons, atoms, and millions of transistors backing me", "I've faeries, pixies, Quempel and Lady Cottington"
    9 hours 7 min ago

Twitter Icon Flickr Icon LinkedIn Icon Facebook Icon Drupal Association, Individual Member icon