Problem & Proposed resolution:

Comments for 3rd parameter of drupal_static() should be changed. See original report for a reason why.

For D8 it is planned to remove the function, but in D7 the function will live on, so the documentation is important. Patch applies do D7 too (with -p2 flag)

Remaining tasks, API changes

none. (This patch intends no changes whatsoever, except for more clarity.)

Original report by hefox

Didn't see anything specially about this and saw that d7 had issues under review to change drupal_static, so figured file here and accept my "duplicate" if it is.

function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
  static $data = array(), $default = array();
  // First check if dealing with a previously defined static variable.
  if (isset($data[$name]) || array_key_exists($name, $data)) {
    // Non-NULL $name and both $data[$name] and $default[$name] statics exist.
    if ($reset) {
      // Reset pre-existing static variable to its default value.
      $data[$name] = $default[$name];
    }
    return $data[$name];
  }
  // Neither $data[$name] nor $default[$name] static variables exist.
  if (isset($name)) {
    if ($reset) {
      // Reset was called before a default is set and yet a variable must be
      // returned.
      /////// This!!! why is it doing this?! ////////
      return $data;
    }
    // First call with new non-NULL $name. Initialize a new static variable.
    $default[$name] = $data[$name] = $default_value;
    return $data[$name];
  }
  // Reset all: ($name == NULL). 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;
}

It's very unexpected behavior and easily leads to bugs. #2314173: Something is overriding the static for workbench_moderation_transitions Is it really suppose to be working that way? It means reset works completily differently if called at different points.

Comments

hefox’s picture

// Neither $data[$name] nor $default[$name] static variables exist.

-- Also, this is incorrect -- it never checks if $default[$name] exists or if $default_value was passed in

longwave’s picture

I think this is considered by design. The documentation for the $reset parameter mentions "Should be used only though via function drupal_static_reset() and the return value should not be used in this case."

Anyway, what else could it return at that point? As drupal_static() always returns a reference, it has to return a known variable - it can't return NULL.

hefox’s picture

I don't think the $reset documentation is clear that that is expected:

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 drupal_static_reset() and the return value should not be used in this case.

If I didn't know about the actual code, my assumption is the third sentence went along with the second -- specially cause it ends with "in this case" not "when using reset"

I'd expect it to return the same that it would return if called for the first time with drupal_static($name, $default_value), e.g $data[$name] = $default_value.

roderik’s picture

Version: 7.x-dev » 8.0.x-dev
Component: base system » documentation
Category: Bug report » Task
Issue summary: View changes
Status: Active » Needs review
Related issues: +#1577902: [META] Remove all usages of drupal_static() & drupal_static_reset()
StatusFileSize
new1.3 KB

I also came to the same conclusion: "Hey, the 3rd sentence goes along with the 1st... but it doesn't read like that." (While I was just browsing around code and trying to 'get' the internal details.)

So, that 2nd sentence should be moved to drupal_static_reset(), then it is more explicit that drupal_static(,,TRUE) should never be called by other code.

(I searched for other wording and found the phrase "This parameter is only used internally and should not be passed in" in D8's file.inc.)

Patch also applies to D7, with patch -p2.

roderik’s picture

Issue summary: View changes

fix html tag

roderik’s picture

Title: drupal_static('name', whatever, TRUE) returns the entire $data array sometimes (e.g. not restricted to name) » Change drupal_static('name', whatever, TRUE) function comments

and title change.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs backport to D7

Looks fine to me, thanks!

alexpott’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed f8c5862 and pushed to 8.0.x. Thanks!

  • alexpott committed f8c5862 on 8.0.x
    Issue #2314181 by roderik | hefox: Change drupal_static('name', whatever...
pushpinderchauhan’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new1.26 KB

Attached is patch for D7.

jhodgdon’s picture

Status: Needs review » Fixed

Thanks again all! Committed to 7.x.

  • jhodgdon committed 5b99e31 on 7.x
    Issue #2314181 by roderik, er.pushpinderrana, hefox: Fix docs for...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.