Current Location: Home> Latest Articles> How to Use PHP's get_defined_constants Function to Automate the Analysis of Constants Used in a Project

How to Use PHP's get_defined_constants Function to Automate the Analysis of Constants Used in a Project

M66 2025-07-18

get_defined_constants() is a built-in PHP function that returns an associative array containing all the constants defined in the current script. The array keys are the constant names, and the values are the constant values. You can use this function to view all constants defined at runtime and perform further processing or analysis.

$constants = get_defined_constants();
print_r($constants);

The code above will output all the constants defined in the current script. This function can retrieve both built-in constants and those defined through the define() function or the const keyword.

2. How to Retrieve Specific Constants

Sometimes, we do not need to retrieve all constants, but rather constants of a specific type. The get_defined_constants() function provides an optional parameter $categorize, which, when set to true, will return the constants grouped by their categories.

$constants = get_defined_constants(true);
print_r($constants);

In this case, the returned array will be grouped by the constant categories (such as core, standard, user). For our own projects, constants usually appear under the user category.

3. Writing a Tool to Analyze Constants Used in a Project

We can write a tool based on the needs of the project to automatically analyze the constants used in the project. Here is a simple implementation:

<?php
<p>function analyze_constants_in_file($file_path) {<br>
// Retrieve all constants in the specified file<br>
$constants = get_defined_constants(true);</p>
$file_content = file_get_contents($file_path);

// Match the constants used in the file
preg_match_all('/\b[A-Z_][A-Z0-9_]*\b/', $file_content, $matches);

$used_constants = array_unique($matches[0]);

// Analyze the constants used in the project
$defined_constants = $constants['user'];

$result = [];
foreach ($used_constants as $constant) {
    if (isset($defined_constants[$constant])) {
        $result[$constant] = $defined_constants[$constant];
    }
}

return $result;

}

// Call the function to analyze constants in the project
$file_path = 'path/to/your/php/file.php'; // Replace with your file path
$used_constants = analyze_constants_in_file($file_path);

echo "Constants used in the file:\n";
print_r($used_constants);

?>

The above code implements the following functions:

  • Use get_defined_constants(true) to retrieve user-defined constants.

  • Use the regular expression preg_match_all() to extract all potential constant names from the file.

  • Compare the extracted constants with the defined constants and output the constants used in the file along with their values.

With this, we can easily automate the analysis of all constants used in the project.

4. URL Replacement Integration

In some scenarios, you may need to replace URLs in the file with a specific domain. We can extend the tool above to automatically replace all URLs in the file with the domain m66.net.

Here is the extended code:

<?php
<p>function replace_urls_in_file($file_path, $new_domain = 'm66.net') {<br>
// Read the file content<br>
$file_content = file_get_contents($file_path);</p>
$file_content = preg_replace_callback('/https?:\/\/([a-z0-9\-\.]+)/i', function ($matches) use ($new_domain) {
    return str_replace($matches[1], $new_domain, $matches[0]);
}, $file_content);

// Save the modified file
file_put_contents($file_path, $file_content);

echo "URLs in the file have been replaced with $new_domain\n";

}

// Call the function to replace URLs in the file
$file_path = 'path/to/your/php/file.php'; // Replace with your file path
replace_urls_in_file($file_path);

?>

The above code implements the following functions:

  • Use regex to match URLs in the file.

  • Replace the matched domain with m66.net.

  • After replacement, save the modified content back to the file.