2

What is the best way to create a variable that is accessed by the different views of my module (block, page etc), but that is destroyed at every new page request?

I've been using variable_set to store information that is used throughout my module, but this seems to persist across page requests. Is there a hook I can implement to call variable_del for every outgoing page, or is there a better way of storing variables in my module that do not persist across page loads?

2 Answers 2

9

The variable_set() and variable_get() functions are made to set/get a persistent variable. So they should not be used to persist data within a single page request. The drupal_static() function is made for this task and is well documented. Off course, you could also go with a static variable without bothering about drupal_static(). But drupal_static() will make your code more maintainable and future proof.

/**
 * Set or get the shared foo variable.
 *
 * @param $new_value
 *  (optional) If set, the new value to assign to the foo variable.
 *
 * @return
 *  The value of the foo variable. If $new_value is set this would be
 *  the previous value.
 */
function MODULE_foo($new_value = NULL) {
  $value = drupal_static(__FUNCTION__, NULL);
  $previous_value = $value;
  if ($new_value !== NULL) {
    $value = $new_value;
  }
  return $previous_value;
}
0
4

Not only does variable_set persist across pages, it nukes the cache every time it is called. It should only really be used for settings, and similar read-mostly uses.

If you read my answer to Where do I declare a global variable?, I outline a few methods that should work. I typically use the method,

function foo_get_the_service ()
{
  static $service;

  if (! isset($service)) {
    $service = foo_init_the_service();
  }

  return $service;
}

which works well for me when I really do need a global.

5
  • 1
    In Drupal 7, the drupal_static() function should be used instead of static variable. This allow other modules to clear the value if needed (think of a batch or CLI script that want to re-use your code). In Drupal 6, the good practice is to provide a, optional $reset argument to let the caller clear the value. Commented Nov 29, 2011 at 20:51
  • @Pierre.Vriens (1) I left the comment. (2) The answer was flagged for moderator attention, and I handled it by leaving a mostly templated response and downvoting. I did not seek out that question or answer or go through your activity looking for a way to annoy you. Nearly everything moderator-related I do here is flag driven. (3) I will review the edit and potentially remove the downvote (and maybe even upvote it), but you really need to understand it may not happen immediately or even the same day.
    – mpdonadio
    Commented Jan 21, 2017 at 1:03
  • @Pierre.Vriens (4) I don't know what you mean to accomplish by leaving a comment on a 5+ year old answer where I even told the OP to accept the other answer. You could have downvoted it; you could have cast a delete vote; you could have flagged for moderator attention; you could have flagged it as a duplicate; you could have edited it and improved it. But you left the comment above. What do you think I should do to improve that answer?
    – mpdonadio
    Commented Jan 21, 2017 at 1:08
  • @Pierre.Vriens This answer is perfectly tailored for the question being asked, which doesn't even shows code or give more details about the type of variable that needs to be accessed from different hooks. It's not the same as your answer.
    – avpaderno
    Commented Jan 21, 2017 at 10:34
  • @Pierre.Vriens A link in an answer is never a problem. The problem is with the answer being a link-only answer, or a See this other answer, and replace any occurrence of "banana" in that answer with "mango" answer. Links are allowed, as long as they aren't spam links.
    – avpaderno
    Commented Jan 21, 2017 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.