Beyond global: Managing Shared State in PHP Functions
In the snippet you provided, the variable $a is technically already global. In PHP, any variable defined outside of a function resides in the Global Scope. The syntax global $a; inside your functions doesn't create a new variable or change the variable's location; it acts as a reference mechanism. It tells the function, "I want to look up $a in the current scope and treat it as if I were creating a local variable with that name."
The reason this feels clunky is that PHP does not support true closures by defaultâvariables defined outside functions are not automatically visible inside them without explicit declaration. This forces you to repeat global $a; every time you need the value, which breaks encapsulation and makes code harder to maintain. You cannot simply declare the variable once and have it implicitly accessible everywhere without some form of scope management.
To achieve a single definition point with accessible values in multiple functions without repeating global, you must change how the data is stored. The standard approach is to utilize a static property within a class or leverage a global array, but for modern PHP development, dependency injection via classes is the robust solution.
The Class Property Pattern
The most scalable way to share state between functions without using global keywords is to encapsulate them within a class. By defining $a as a property of a class, you declare it once. Any method (function) inside that class can access it directly. If you need these functions to exist outside the class, you can instantiate the class and pass an instance into those external functions.
Here is how you would refactor your code using a class:
class AppContext {
public $a = "localhost";
public function body() {
// Direct access to the property; no 'global' keyword needed
echo $this->a;
}
public function head() {
echo $this->a;
}
public function footer() {
echo $this->a;
}
}
// Usage
$context = new AppContext();
$context->body();
$context->head();
If your functions are defined at the global level and you cannot move them inside a class, you must still instantiate an object and pass it around. While this requires passing the object as an argument (e.g., func($context)), it eliminates the need for repetitive global declarations and prevents accidental modification of unrelated global variables.
Why Not Use a Global Array?
A common workaround when moving to classes isn't immediately possible is using a global array, such as $GLOBALS['a']. While this technically allows you to define the variable once and access it anywhere via $GLOBALS['a'], it is widely discouraged in professional PHP development.
Relying on $GLOBALS couples your functions tightly to the execution environment and makes testing significantly harder. If you want to pass a mock object into a unit test, you have to modify global state or use complex workarounds. The global $var keyword is explicit about where data lives; using $GLOBALS hides that connection behind an array lookup, reducing code readability.
When to Use global Correctly
You should not feel ashamed of using global if you are working in a legacy codebase or a constrained environment where refactoring into classes is impossible. However, understand that global is a reference, not a declaration of location. The variable exists globally regardless. The keyword simply allows the function to interact with that external scope.
If you are building new applications, particularly those on modern stacks like Laravel, the framework's service container handles this dependency injection automatically. You define services (and their properties) once in the container, and they are injected into your controllers or classes as needed. This pattern scales infinitely better than manually managing global variables.
Ultimately, if you want to avoid repeating global, the answer is not a magic keyword, but a structural shift: move shared state into class properties and inject instances where that state is required.
