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.
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | drupal7-static-comments-2314181-10.patch | 1.26 KB | pushpinderchauhan |
| #4 | drupal-static-comments-2314181-4.patch | 1.3 KB | roderik |
Comments
Comment #1
hefox commented// 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
Comment #2
longwaveI 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.
Comment #3
hefox commentedI don't think the $reset documentation is clear that that is expected:
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.
Comment #4
roderikI 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.
Comment #5
roderikfix html tag
Comment #6
roderikand title change.
Comment #7
jhodgdonLooks fine to me, thanks!
Comment #8
alexpottCommitted f8c5862 and pushed to 8.0.x. Thanks!
Comment #10
pushpinderchauhan commentedAttached is patch for D7.
Comment #11
jhodgdonThanks again all! Committed to 7.x.