Current Location: Home> Latest Articles> Dynamically detect newly defined constants (before and after comparison)

Dynamically detect newly defined constants (before and after comparison)

M66 2025-05-18

In PHP programming, constants are identifiers that cannot be changed during execution and are often used for declarations of configuration parameters or fixed values. When we are developing large applications or debugging third-party libraries, we sometimes need to detect which constants are defined or modified in a certain piece of code. At this time, the get_defined_constants() function comes in handy.

get_defined_constants() is a PHP built-in function that returns all currently defined constants and their corresponding values. By calling the function before and after the code is executed and comparing, we can accurately capture the newly added constants, and even further check the values ​​of these constants.

1. Basic syntax

 array get_defined_constants ([ bool $categorize = false ] )
  • If the $categorize parameter is set to true , the returned array will be grouped according to the classification of constants (such as core, user-defined, etc.);

  • Otherwise, return a flat one-dimensional associative array, the key is the name of the constant and the value is the value of the constant.

2. Dynamically detect newly defined constants

We can get the new constants added in the intermediate code by calling get_defined_constants() once before and after a certain piece of code, and then comparing the differences between the two arrays.

Here is a complete example:

 <?php

// Record the initial constant state
$before = get_defined_constants();

// Simulate loading third-party configuration files or libraries
include 'http://m66.net/sample-config.php'; // Example URL

// Record the state of constants after loading
$after = get_defined_constants();

// Calculate new constants
$new_constants = array_diff_assoc($after, $before);

// New constants are added to the output
echo "The new constants are as follows:\n";
foreach ($new_constants as $name => $value) {
    echo "$name => " . var_export($value, true) . "\n";
}

In this example, we use array_diff_assoc() to compare the differences between two arrays, which not only compares the key names, but also the key values. This allows you to get exactly those newly defined constants, rather than existing constants whose values ​​are changed.

3. Advanced use: Filter user-defined constants

If you only want to get user-defined constants, you can add classification parameters when calling get_defined_constants(true) and extract the constants under the "user" category from it. For example:

 $before = get_defined_constants(true)['user'] ?? [];
include 'http://m66.net/sample-config.php';
$after = get_defined_constants(true)['user'] ?? [];

$new_constants = array_diff_assoc($after, $before);

foreach ($new_constants as $name => $value) {
    echo "$name => " . var_export($value, true) . "\n";
}

This method is especially suitable for scenarios where the system default constants need to be excluded, and can focus on capturing new definitions in user or business logic.

IV. Application scenarios

  1. Debugging third-party libraries : analyze what constants are introduced by a library or framework;

  2. Configuration Tracking : Position duplicate or conflicting constants defined in multiple configuration files;

  3. Security audit : monitor whether any abnormal constants are maliciously injected;

  4. Testing and Verification : Comparison of the status differences before and after in the automatic test process.

5. Things to note

  • Once a constant definition cannot be modified, repeated definitions will cause a warning;

  • It is recommended to use namespace prefix to avoid naming conflicts;

  • Avoid dynamically generating a large number of constants in core logic, otherwise it will affect the maintainability of the code;