`static` keyword inside function?

`static` keyword inside function?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Hidden State Machine: static Variables in PHP

When examining the source code for Drupal 7, specifically within functions like module_load_all, you will encounter a declaration that often trips up developers new to the language:

function module_load_all($bootstrap = FALSE) {
    static $has_run = FALSE;
    // ... rest of function
}

At first glance, $has_run looks like a standard local variable. It is declared inside the function scope, which typically means it exists only for the duration of that single execution context. However, the static keyword fundamentally alters this behavior, turning a transient variable into a persistent piece of state that survives across multiple calls to the same function.

How Scope Works Without static

To understand why static is necessary here, consider what happens if you remove the keyword:

function simple_counter() {
    $count = 0;
    return ++$count;
}

echo simple_counter(); // Outputs 1
echo simple_counter(); // Outputs 2

In this scenario, $count is created anew every time simple_counter() is invoked. Once the function finishes executing, the variable is destroyed. The next call starts fresh with zero. This is standard local scope behavior.

However, in the Drupal example, we want to track whether the function has already executed its main logic (loading modules) during the current request lifecycle. If module_load_all() runs twice within the same page load, we don't want to reload the modules again; that would be inefficient and potentially cause issues. We need $has_run to remember its value between calls without using a global variable or passing an external flag.

The Mechanism of Persistence

When you declare a variable as static, PHP initializes it only once, typically on the first invocation of the function. Subsequent calls reuse that same memory location. Effectively, the variable lives in the function's scope but persists until the script terminates (or is garbage collected if the execution flow resets, though in CLI or web contexts, it usually lasts until the request ends).

This pattern creates a "state machine" inside the function. The first call sets $has_run to FALSE, executes the heavy lifting (loading modules), and flips $has_run to TRUE. Every subsequent call sees $has_run as TRUE and skips the loading logic immediately.

Implementation Trade-offs and Pitfalls

While this pattern is elegant for avoiding global variables, it introduces specific considerations you must keep in mind when refactoring or maintaining code.

1. The Single-Request Constraint
Static variables inside functions persist only within the context of a single script execution. In a web environment like Laravel or Drupal, this means the variable resets between HTTP requests. It does not store state across sessions or pages unless you explicitly manage that persistence elsewhere (e.g., in a session or database). If you rely on static variables to track data across different users or requests, you will encounter bugs because each request starts with a "clean slate."

2. Debugging Complexity
Because the variable is hidden inside the function scope, it can be harder to inspect during debugging. Standard stack traces show the call stack, but they don't always reveal the current value of static variables without stepping through the code or using specific debugger features. If a bug manifests only on the second call to a function, tracing the issue requires understanding that the variable's value has changed from FALSE to TRUE.

3. Refactoring Risks
If you move the logic containing the static variable into a different file or class, you must ensure the static declaration remains intact. Moving the definition outside the function will destroy the intended behavior. Furthermore, if you are migrating legacy code (like Drupal 7) to modern frameworks like Laravel, you might be tempted to replace this with dependency injection or service container methods. While that is often a better architectural choice for large applications, the static pattern remains valid for small, self-contained utilities where introducing external dependencies adds unnecessary complexity.

When to Use It

This specific implementation in Drupal is a classic example of the "init-once" pattern. It is ideal for functions that perform expensive operations (like loading configuration or registering hooks) only when necessary. By using static, you avoid the overhead of repeated initialization without polluting the global namespace.

However, always ask yourself: does this function need to remember something between calls? If the answer is yes, and you don't want to use a class property or a global, static is your tool. But be wary of overusing it; excessive reliance on hidden state can make code harder for teammates to understand and maintain.

Our network