how to define global constants in codeigniter in constants.php

how to define global constants in codeigniter in constants.php

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

Stefan Bogdanescu

Founder & Senior Architect

Defining Global Constants in CodeIgniter: Beyond constants.php

In CodeIgniter, there is a persistent debate about where to place global constants. Developers often default to application/config/constants.php because it sounds like the most logical place for immutable values. However, relying on this file alone can lead to runtime errors and confusing behavior if not handled correctly. The core issue usually stems from how PHP's define() function interacts with autoloading and the framework's configuration system.

The Pitfall of constants.php

Your source material shows a common pattern: defining roles like SuperAdmin, Admin, and RestaurantUser using define() inside a file named constants.php. While this approach works in a standalone script, it breaks down in a framework environment like CodeIgniter.

The primary problem is execution order. When you include config/constants.php early in your application lifecycle, the constants are loaded into the global scope. However, if you attempt to access these constants before they are defined, or if you try to override them later, PHP will throw a fatal error stating that the constant already exists. In CodeIgniter, this often manifests when a library or helper tries to use a constant that was supposed to be defined in the config file but wasn't actually loaded into the current environment context correctly.

Furthermore, using define() inside a file included via CI_Loader::config() is redundant and fragile. The Config class in CodeIgniter is designed to load arrays from application/config/ files. If you put raw define() calls inside that array, they are executed immediately upon loading the config file. This can cause issues if your application structure changes or if you need to conditionally define constants based on environment settings (e.g., development vs. production).

The Correct Approach: Using Arrays

The robust solution is to stop using define() directly in your configuration files and instead use arrays. CodeIgniter's Config class automatically converts array keys into constant names when the config file is loaded.

Instead of this:

// In constants.php (problematic)
$config['SuperAdmin'] = '1';
$config['Admin'] = '2';

You should define your values directly in an associative array within application/config/constants.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

// Define constants as keys in an array. 
// CodeIgniter will automatically convert these to constants when the file is loaded.
$constants = [
    'SuperAdmin' => '1',
    'Admin' => '2',
    'CityAdmin' => '3',
    'SupportAdmin' => '4',
    'RestaurantUser' => '5',
    'FrontUser' => '6',
    'Other' => '7',
];

However, there is a nuance here. Simply assigning to $config inside the file does not automatically trigger the creation of global constants named after those keys unless you explicitly tell the Config class to load it as constants or if your loading method handles it specifically. The most reliable method in modern CodeIgniter versions (3.x and 4.x) is to define these as standard PHP constants directly in a separate file, but ensure they are loaded after the framework initializes, or use the constants config group correctly.

A safer pattern often used by experienced developers is to place these definitions in a dedicated file that is autoloaded or manually included after the core is initialized:

// application/config/constants.php (Alternative approach)
define('SuperAdmin', '1');
define('Admin', '2');
define('CityAdmin', '3');
define('SupportAdmin', '4');
define('RestaurantUser', '5');
define('FrontUser', '6');
define('Other', '7');

This works fine if constants.php is included early. But to avoid the "already defined" error, you must ensure you are not defining them multiple times. If your framework loads this file twice (once in a library and once in a controller), the second run will crash. To prevent this, always wrap definitions in a check:

if (!defined('SuperAdmin')) {
    define('SuperAdmin', '1');
}

Accessing Constants in Controllers

Once defined, accessing these constants in a controller is straightforward because they exist in the global scope. You do not need to pass them through sessions or view data unless you intend to display them.

In your controller logic:

class Auth extends CI_Controller {
    public function check_role()
    {
        // Accessing the constant directly
        if ($this->input->post('role_id') == SuperAdmin) {
            // Logic for super admin
        } elseif ($this->input->post('role_id') == Admin) {
            // Logic for admin
        }
    }
}

Why Not Use Arrays in Controllers?

You might ask, "Why not just pass an array of roles from the model to the controller?" The answer lies in maintainability and consistency. Constants provide a single source of truth for static values like role IDs. If you decide to change Admin from '2' to '3', you only need to update it in one place. If that value is scattered across multiple arrays in models, controllers, and views, you risk introducing bugs during refactoring.

If you are transitioning from an older CodeIgniter setup where constants.php caused issues, the fix is often simply ensuring that file is included exactly once, typically at the very beginning of your application initialization or within a bootstrap file. For newer projects, consider using dependency injection for dynamic roles and reserving constants only for truly immutable identifiers like role codes or system flags.

For deeper insights into managing configuration in complex applications, you might explore how modern frameworks handle environment-specific constants, as discussed by experts at https://laravelcompany.com.

Our network