CodeIgniter - best place to declare global variable

CodeIgniter - best place to declare global variable

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

Stefan Bogdanescu

Founder & Senior Architect

Where to Declare Global Variables in CodeIgniter

Using a single variable across controllers, views, and configuration files like routes.php in CodeIgniter is an anti-pattern that fights against the framework's core architecture. The request to bypass standard mechanisms like the Config class or get_instance() often stems from a desire for simplicity or a misunderstanding of how autoloading and dependency injection work in modern PHP frameworks.

If you attempt to define a global variable in the bootstrap sequence, you will likely encounter significant friction. CodeIgniter's autoloaders expect specific classes and libraries to be present before they are referenced. If you rely on a bare PHP variable defined in index.php or an early bootstrap file, you risk creating tight coupling where your configuration depends on runtime state that hasn't been initialized yet.

The Bootstrap Reality

To understand where to place code in CodeIgniter, you must look at the loading order found in the standard system/core/index.php. This file is the entry point, but it immediately delegates control to application/init.php. This is the first file specifically designed for application-level initialization logic.

You could technically define a variable here. However, doing so creates a fragile global state. By the time this file executes, CodeIgniter has not yet loaded its configuration array or initialized the database connection. Defining a "global" variable here means it exists in the superglobal $GLOBALS array before the framework has established its own context. If your routes.php or controller logic attempts to use that variable before the framework is fully booted, you will encounter undefined index errors or unexpected behavior.

Why Globals Are Problematic

The primary reason to avoid global variables is that they escape the control of the Dependency Injection (DI) container. Modern frameworks, including CodeIgniter 4 and Laravel, encourage passing data explicitly via constructor arguments or helper functions. This makes testing trivial; you can swap out a mock object without touching the class definition. With a global variable, every single file must know about that specific variable name to access it.

Furthermore, globals are notoriously difficult to debug. When an error occurs, the stack trace rarely indicates where a value was set or modified, making it impossible to track down race conditions or stale data issues. In CodeIgniter 3, while the framework is less opinionated than CI4 about DI containers, the principle remains: explicit dependencies are superior to implicit globals.

The Correct Approach: Configuration Classes

Instead of forcing a variable into every file, you should use the Config class or, better yet, create a dedicated configuration class for your specific needs. CodeIgniter provides a Config loader that can handle complex structures and ensure they are loaded at the appropriate time.

If you need a value accessible in routes.php, controllers, and views, define it in an external PHP file within your application root, such as app/Config/MyApp.php. You then load this configuration where needed using the framework's built-in methods. For example:

// app/config/my_app.php
return [
    'my_variable' => 'specific_value',
];

Then, in your controllers or routes:

$loader = \Config\Services::config(); // Or use Services::database() patterns if available
$config = $loader->get('MyApp'); 
echo $config['my_variable'];

In CodeIgniter 3, you can also utilize the load method in your controllers to access these settings cleanly. This approach keeps your code self-contained and avoids polluting the global namespace.

When Autoloaders Are Not Enough

If you absolutely require a variable to be available before any controller logic runs, including during route definition, consider placing it in a static property of a configuration class rather than a global variable. Static properties are effectively global within the scope of that class but remain typed and scoped, preventing namespace pollution.

class MyGlobalConfig {
    public static $variable = 'value';
}

Access this anywhere with MyGlobalConfig::$variable. This pattern offers the convenience you are looking for without abandoning the safety nets provided by the framework's architecture. It allows you to avoid the overhead of get_instance() while maintaining a clean separation between business logic and configuration data.

Ultimately, relying on globals undermines the maintainability of your application. By leveraging the Config class or static properties within custom configuration classes, you achieve global accessibility without sacrificing the structural integrity that CodeIgniter provides.

Our network