Current Location: Home> Latest Articles> Comparison of alternative relationship between constants and configuration arrays

Comparison of alternative relationship between constants and configuration arrays

M66 2025-05-19

In PHP development, we often use constants to define some global configurations, such as paths, switches, service interfaces, etc. But in more modern or modular architectures, configuration arrays (such as config.php or .env environment variables) gradually replace a large number of global constant usage. To smooth migration or make compatibility comparisons, we may need to compare the constants defined in the current run environment to see if they can be replaced by values ​​in the configuration array.

At this time, the get_defined_constants() function comes in handy. It can return all defined constants (including system and user-defined constants) and present them in an array, which facilitates us to perform logical processing.

Here is a practical example that shows how to compare with the configuration array via get_defined_constants() and find out those parts that may have duplicates or conflicts.

 <?php
// Simulate a configuration array,It is usually possible from config.php or env File loading
$config = [
    'APP_ENV' => 'production',
    'API_URL' => 'https://api.m66.net',
    'DEBUG'   => false,
];

// Assume that some configurations are originally constant
define('APP_ENV', 'production');
define('API_URL', 'https://api.m66.net');
define('SITE_NAME', 'MySite');

// Get all user-defined constants
$userConstants = get_defined_constants(true)['user'];

// Used to store matching results
$matched = [];
$unmatched = [];
$missingInConstants = [];

foreach ($config as $key => $value) {
    if (array_key_exists($key, $userConstants)) {
        if ($userConstants[$key] === $value) {
            $matched[$key] = $value;
        } else {
            $unmatched[$key] = [
                'constant' => $userConstants[$key],
                'config'   => $value
            ];
        }
    } else {
        $missingInConstants[$key] = $value;
    }
}

// Output comparison results
echo "=== Matched configuration items(Constant and value are consistent) ===\n";
print_r($matched);

echo "\n=== 不Matched configuration items(The same name but different values) ===\n";
print_r($unmatched);

echo "\n=== Items that exist in the configuration but are not defined as constants ===\n";
print_r($missingInConstants);

Code description:

  1. Get user constants:
    get_defined_constants(true)['user'] can get all user-defined constants, this step is key.

  2. Comparative logic:
    Iterate through the configuration array, check whether each configuration item exists in a constant, and if it exists, the comparison values ​​are consistent.

  3. Classification output:

    • Exactly matched terms indicate that they have been consistent with the configuration value through constants;

    • A mismatched term means there is potential conflict;

    • Missing entries indicate that these configurations have not been moved to constants or are intentionally not defined as constants.

Practical application scenarios:

  • Migration Support: When you are preparing to migrate from old constant definitions to a unified configuration file structure, you can use this method to confirm which constants have been overwritten by the configuration array.

  • Debugging purpose: Track the repeated definition of constants and configurations in large projects to prevent logical overwrites and confusion.

  • Environment verification: This kind of comparison mechanism can be embedded in automated deployment scripts to ensure that predefined constants and configuration files are consistent.

In general, comparing get_defined_constants() with configuration arrays is a very practical code auditing technique, especially suitable for use when refactoring old projects. If you are maintaining a PHP project that is gradually migrating from constant configuration to centralized configuration, this method will save you a lot of troubleshooting and maintenance time.