0

For persisting variables in my module (without using the db) I have these options:

  • variable_set/variable_get - for general use
  • drupal_static - should be used for static variables
  • define - should be used for constants

Is this an accurate summary? Are there other methods I'm missing?

  • 1
    Very, very short version: variable_get/set do use the database (variable table specifically, though it can be moved to a memory storage like memcache/redis). drupal_static is for stuff that needs to be checked every time (may have changed), but may also be queried twice in the same page request, so the expensive op is only performed once. Constants are for values needed in code that won't change per-request. Core doesn't provide any other methods I don't think, but contrib modules like memcache and redis do when used with the complementary php plugin and server – Clive Jan 5 '16 at 18:26
  • you can use global if you don't want it to persist over multiple requests – nikunj Jan 6 '16 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.