4

I'm trying to define a static variable for use in my custom module. I want it to let me pass a number from function to function. I'm using drupal_static() but I can't get it to work. How do I do this?

This is the code I'm using to define my static variable.

$var = &drupal_static(__FUNCTION__);
if (!isset($var)) {
  // generate contents of static variable
  $var = '8';
}
return $var;

I've tried putting it in a custom module.

 function viewusercontact_locateuser($value, $key, &$counter) {
   global $user;

   /**********In here***********/

   if ($value['uid'] == $user->uid) {
      ........
   }
}

I can't seem to get it to work. What am I doing wrong?

(I got my code from the following guides)

https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_static/7 http://www.lullabot.com/blog/article/beginners-guide-caching-data-drupal-7 http://erikwebb.net/blog/drupal-design-patterns/static

8

drupal_static() stores variables based on the name of the function they're related to (hence the use of __FUNCTION__ when you initialise it in the relevant function).

You can pick it up again in any other function by querying drupal_static() again, with the name of the originating function. So in your case:

function viewusercontact_foo() {
  $static_var = drupal_static('viewusercontact_locateuser');
  ...
}
  • If that's not what you meant just drop me a comment – Clive Sep 17 '13 at 21:18
  • That does work, thank you, I didn't know you had to define it in another function. Unfortunately the reason I tried to define it in function viewusercontact_locateuser is because it's a value from that function I want to pass around. Is that possible? Am I going to have to store it in $_SESSION or $GLOBALS? – Dominic Woodman Sep 17 '13 at 21:33
  • Ah ok I thought that might be it - just so I'm sure before updating the code, you're essentially looking for somewhere in-memory to store the value contained in the static var, so that once it's been generated once it can be picked up in the same function, but also in another function that runs sometime after? – Clive Sep 17 '13 at 21:38
  • No, never $GLOBALS if you can help it :) $_SESSION feels like overkill as you don't want to persist the data across a page request. Static vars are the right way to go, it just how you do it depends on exactly what you want to do – Clive Sep 17 '13 at 21:39
  • 2
    Oh hang on a minute I'm being a bit thick - once you've set it using drupal_static() you can pick that same var up again in another function using $var_from_other_function = drupal_static('viewusercontact_locateuser'); – Clive Sep 17 '13 at 21:40
5

Here's a write up of my final solution.

I solved this problem by avoiding it completely. But the solution to the problem posted is below (as solved by Clive). First you have to define a static variable in a separate function:

function myvariable_function() {
  $var = &drupal_static(__FUNCTION__);
      if (!isset($var)) {
        // generate contents of static variable
        $var = '8';
      }
    return $var;
}

Then in the first place you use the variable you need it you have to call the function.

$var = myvariable_function();

From then on you can call it from other functions, by referencing the function.

function my_other_function () {
  $dom =  &drupal_static('myvariable_function');
}

In order to change that value in another function you can set:

$dom = $something_else;

And then when you call it again with &drupal_static, it will be updated:

$newvar = &drupal_static('myvariable_function');
0

You can't pass a static variable value from a function to another. The scope of a static variable is the function where it is declared. It keeps its value between all calls to same function in the same request.

  • 1
    With normal static vars that's true, with drupal_static() you can simulate it – Clive Sep 17 '13 at 21:45

Your Answer

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

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