Static cache does not work properly in locale_supported_languages

bmihelac - May 13, 2008 - 04:45
Project:Drupal
Version:5.7
Component:locale.module
Category:bug report
Priority:normal
Assigned:Unassigned
Status:patch (code needs work)
Description

Static variables are used to cache $all and $enabled languages with unset() function to reset cache. unset() function just unsets variable inside function and PHP reset its value next time the function is called. This way after reset return value is same as before reset.

Here is example how static caching works with unset():

<?php
function locale_supported_languages($reset = FALSE, $val = array()) {
  static
$all = NULL;

  if (
$reset) {
   
// If a static variable is unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable.
    // unset($all);
   
$all = NULL;
  }
 
  if (
is_null($all)) {
   
$all = array();
    foreach (
$val as $key => $value) {
     
$all[$key] = $value;
    }
  }
  return
$all;
}

echo
"\narray should have 'cached' element (PASS):";
print_r(locale_supported_languages(FALSE, array("first" => "cached")));

echo
"\narray should have 'new' element (PASS):";
print_r(locale_supported_languages(TRUE, array("first" => "new")));

echo
"\narray should have 'new' element but have 'cached' (FAILS):";
print_r(locale_supported_languages(FALSE));
?>

Solution is easy, just set variable to NULL instead unsetting it.

AttachmentSize
locale_supported_languages_static_cache_bug.patch668 bytes
 
 

Drupal is a registered trademark of Dries Buytaert.