Why don't I get a value from drupal_static()? This post is basically a duplicate question of one I asked a little while back: Using Static Variables in a Custom Module. Unfortunately I can't get my own solution to work.

I'm trying to use a static variable in a custom module.

function tellafriend_userfullname_function() {
  global $user;
  $var = &amp;drupal_static(__FUNCTION__);

  if (!isset($var)) {
    // generate contents of static variable
    $var = user_load($user-&gt;uid);
  }

  return $var;
}


The static variable will be used in the form submission handler and in hook_mail().

function tellafriend_form_submit($form, &amp;$form_state) {
  $var = &amp;drupal_static('tellafriend_userfullname_function');
  $params = array();

  if ($form_state['values']['cv_included'] == '1') {
    $params['1'] =  array('uri' =&gt; $var-&gt;field_cv_upload['und']['0']['uri'], 'filename' =&gt; $var-&gt;field_cv_upload['und']['0']['filename'],);
  };

  // Other email set-up goes here.

  $message = drupal_mail('tellafriend', 'notify', $email, language_default(), $params);
}

function tellafriend_mail($key, &amp;$message, $params) {
   $message['headers']['MIME-Version'] = '1.0';
   $message['headers']['Content-Type'] = 'multipart/mixed;';
   $message['body'][] = $var-&gt;field_full_name['und']['0']['safe_value'];
}


What have I done wrong?
The default value returned by drupal_static() is NULL; this means that, when you call the function passing for $name a value that has not been used from another function, the value associated with that name is NULL, or the second argument passed to drupal_static().

In your case, you need to call tellafriend_userfullname_function() twice: once to initialize the static value, and once to get the static value.

function tellafriend_form_submit($form, &amp;$form_state) {
  $var = tellafriend_userfullname_function();
  $params = array();

  if ($form_state['values']['cv_included'] == '1') {
     $params['1'] = array('uri' =&gt; $var-&gt;field_cv_upload['und']['0']['uri'], 'filename' =&gt; $var-&gt;field_cv_upload['und']['0']['filename'],);
  };

  // Other email set-up goes here.

  $message = drupal_mail('tellafriend', 'notify', $email, language_default(), $params);
}

function tellafriend_mail($key, &amp;$message, $params) {
  $var = tellafriend_userfullname_function();

  $message['headers']['MIME-Version'] = '1.0';
  $message['headers']['Content-Type'] = 'multipart/mixed;';
  $message['body'][] = $var-&gt;field_full_name['und']['0']['safe_value'];
}


In this case, I would not use drupal_static(), for two reasons.


The value you set and get from drupal_static() is a value you can get from a global variable
Even if that would not be the case, you could simply use a static variable inside tellafriend_userfullname_function(), since drupal_static() is used for a static value that needs to be reset externally from the function that defines it


If you look at the code of drupal_bootstrap(), you will notice the following comments.

  // Not drupal_static(), because the only legitimate API to control this is to
  // call drupal_bootstrap() with a new phase parameter.
  static $final_phase;
  // Not drupal_static(), because it's impossible to roll back to an earlier
  // bootstrap state.
  static $stored_phase = -1;


drupal_static() is not a replacement for every static variable that could be used from a function; it should be used when the static variable is used to save a state another function could need to reset. The documentation for drupal_static() says:


  A guideline is that if a function's static variable does not depend on any information outside of the function that might change during a single page request, then it's ok to use the "static" keyword instead of the drupal_static() function.


As example of when drupal_static() is used, see menu_reset_static_cache(), which contains the following code.

  drupal_static_reset('_menu_build_tree');
  drupal_static_reset('menu_tree');
  drupal_static_reset('menu_tree_all_data');
  drupal_static_reset('menu_tree_page_data');
  drupal_static_reset('menu_load_all');
  drupal_static_reset('menu_link_get_preferred');


If menu_tree() didn't use drupal_static(), resetting the static variable it uses would not be possible, if not changing the function to use the following code.

function menu_tree($menu_name = NULL, $reset = FALSE) {
  static $menu_output = array();

  if ($reset) {
    $menu_output = array();
  }

  if (isset($menu_name)) {
    if (!isset($menu_output[$menu_name])) {
      $tree = menu_tree_page_data($menu_name);
      $menu_output[$menu_name] = menu_tree_output($tree);
    }
  }

  return $menu_output[$menu_name];
}


This would have been possible, but it would have changed the interface between the function and the functions using it. Since this is a common pattern among different Drupal functions, defining drupal_static() and using it in those functions needing it has been a Smart Thing™.