summaryrefslogtreecommitdiff
path: root/includes/bootstrap.inc
diff options
context:
space:
mode:
Diffstat (limited to 'includes/bootstrap.inc')
-rw-r--r--includes/bootstrap.inc79
1 files changed, 79 insertions, 0 deletions
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 803212c14..f0a5ec12a 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -2086,6 +2086,83 @@ function registry_rebuild() {
/**
* Central static variable storage.
*
+ * All functions requiring a static variable to persist or cache data within
+ * a single page request are encouraged to use this function unless it is
+ * absolutely certain that the static variable will not need to be reset during
+ * the page request. By centralizing static variable storage through this
+ * function, other functions can rely on a consistent API for resetting any
+ * other function's static variables.
+ *
+ * Example:
+ * @code
+ * function language_list($field = 'language') {
+ * $languages = &drupal_static(__FUNCTION__);
+ * if (!isset($languages)) {
+ * // If this function is being called for the first time after a reset,
+ * // query the database and execute any other code needed to retrieve
+ * // information about the supported languages.
+ * ...
+ * }
+ * if (!isset($languages[$field])) {
+ * // If this function is being called for the first time for a particular
+ * // index field, then execute code needed to index the information already
+ * // available in $languages by the desired field.
+ * ...
+ * }
+ * // Subsequent invocations of this function for a particular index field
+ * // skip the above two code blocks and quickly return the already indexed
+ * // information.
+ * return $languages[$field];
+ * }
+ * function locale_translate_overview_screen() {
+ * // When building the content for the translations overview page, make
+ * // sure to get completely fresh information about the supported languages.
+ * drupal_static_reset('language_list');
+ * ...
+ * }
+ * @endcode
+ *
+ * In a few cases, a function can have certainty that there is no legitimate
+ * use-case for resetting that function's static variable. This is rare,
+ * because when writing a function, it's hard to forecast all the situations in
+ * which it will be used. A guideline is that if a function's static variable
+ * does not depend on any information outside of the function that might change
+ * during a single page request, then it's ok to use the "static" keyword
+ * instead of the drupal_static() function.
+ *
+ * Example:
+ * @code
+ * function actions_do(...) {
+ * // $stack tracks the number of recursive calls.
+ * static $stack;
+ * $stack++;
+ * if ($stack > variable_get('actions_max_stack', 35)) {
+ * ...
+ * return;
+ * }
+ * ...
+ * $stack--;
+ * }
+ * @endcode
+ *
+ * In a few cases, a function needs a resettable static variable, but the
+ * function is called many times (100+) during a single page request, so
+ * every microsecond of execution time that can be removed from the function
+ * counts. These functions can use a more cumbersome, but faster variant of
+ * calling drupal_static(). For benchmarks and background on this variant,
+ * please see http://drupal.org/node/619666.
+ *
+ * Example:
+ * @code
+ * function user_access($string, $account = NULL) {
+ * // Use the advanced drupal_static() pattern, since this is called very often.
+ * static $drupal_static = array();
+ * isset($drupal_static[__FUNCTION__]) || ($drupal_static[__FUNCTION__] = &drupal_static(__FUNCTION__));
+ * $perm = &$drupal_static[__FUNCTION__];
+ * ...
+ * }
+ * @endcode
+ *
* @param $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)
@@ -2102,6 +2179,8 @@ function registry_rebuild() {
*
* @return
* Returns a variable by reference.
+ *
+ * @see drupal_static_reset()
*/
function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
static $data = array(), $default = array();