You are here

function variable_static in Variable 6

Same name and namespace in other branches
  1. 7.2 variable.module \variable_static()
  2. 7 variable.module \variable_static()

Central static variable storage, Drupal 7 core backport

See http://api.drupal.org/api/function/drupal_static/7

8 calls to variable_static()
variable_global_get in ./variable.module
Get original global variable
variable_global_reset in ./variable.module
Reset global variables to original values
variable_global_set in ./variable.module
Switch global variable
variable_group in ./variable.module
Get variable group information
variable_info in ./variable.module
Get variable information

... See full list

File

./variable.module, line 264
Variable API module

Code

function &variable_static($name, $default_value = NULL, $reset = FALSE) {
  static $data = array(), $default = array();
  if (!isset($name)) {

    // All variables are reset. This needs to be done one at a time so that
    // references returned by earlier invocations of drupal_static() also get
    // reset.
    foreach ($default as $name => $value) {
      $data[$name] = $value;
    }

    // As the function returns a reference, the return should always be a
    // variable.
    return $data;
  }
  if ($reset) {

    // The reset means the default is loaded.
    if (array_key_exists($name, $default)) {
      $data[$name] = $default[$name];
    }
    else {

      // Reset was called before a default is set and yet a variable must be
      // returned.
      return $data;
    }
  }
  elseif (!array_key_exists($name, $data)) {

    // Store the default value internally and also copy it to the reference to
    // be returned.
    $default[$name] = $data[$name] = $default_value;
  }
  return $data[$name];
}