You are here

public static function Vars::staticValue in Variable API 6

Same name and namespace in other branches
  1. 6.2 vars.module \Vars::staticValue()
  2. 7.2 vars.classes.inc \Vars::staticValue()
  3. 7 vars.module \Vars::staticValue()

Central static variable storage.

This is the back port of drupal_static() defined in Drupal 7.

Parameters

$name: Globally unique name for the variable. For a function with only one static variable, the function name (e.g. via the PHP magic __FUNCTION__ constant) is recommended. For a function with multiple static variables add a distinguishing suffix to the function name for each one.

$default_value: Optional default value.

$reset: TRUE to reset a specific named variable, or all variables if $name is NULL. Resetting every variable should only be used, for example, for running unit tests with a clean environment. Should be used only though via function vars_static_reset() and the return value should not be used in this case.

Return value

Returns a variable by reference.

3 calls to Vars::staticValue()
Vars::offsetGet in ./vars.module
Implements ArrayAccess::offsetGet().
Vars::staticReset in ./vars.module
Resets one or all centrally stored static variable(s).
VarsBaseTestCase::testBaseFunctions in tests/vars.test

File

./vars.module, line 421
Implement an API to handle persistent variables.

Class

Vars
@file Implement an API to handle persistent variables.

Code

public static function &staticValue($name, $default_value = NULL, $reset = FALSE) {
  static $data = array(), $default = array();
  if (!isset($name)) {
    foreach ($default as $name => $value) {
      $data[$name] = $value;
    }
    return $data;
  }
  if ($reset) {
    if (array_key_exists($name, $default)) {
      $data[$name] = $default[$name];
    }
    else {
      return $data;
    }
  }
  elseif (!array_key_exists($name, $data)) {
    $default[$name] = $data[$name] = $default_value;
  }
  return $data[$name];
}