In PHP, get_defined_constants() is a very practical function that can get all currently defined constants and return a multidimensional array, usually containing constants under multiple namespaces. These constants are organized in a nested array structure. To traverse these nested structures and process the values of each constant, array_walk_recursive() is a very suitable tool.
This article will explain how to use these two functions in combination, extract and process all constant values, and give actual code examples to demonstrate how to format constant values, or operate on specific types of values.
When the get_defined_constants() function has no parameters, the structure is as follows:
[
"Core" => [
"E_ERROR" => 1,
"E_WARNING" => 2,
...
],
"date" => [
"DATE_ATOM" => "Y-m-d\TH:i:sP",
...
],
...
]
Since this is a multi-dimensional array, we cannot directly operate on each of the values, but need to use recursive methods.
array_walk_recursive() accepts two parameters:
The input array (must be multidimensional)
Callback function, performs certain operations on each value in the array
grammar:
array_walk_recursive(array &$array, callable $callback);
Here is a sample code that demonstrates how to use get_defined_constants() to get all constants and use array_walk_recursive() to iterate through all values and process them.
<?php
$constants = get_defined_constants(true); // Get all constants,Classified by extension
array_walk_recursive($constants, function($value, $key) {
// Suppose we only process string constants,For example, output a domain nameURL
if (is_string($value)) {
// Here we make a simple judgment and replace URL The domain name is m66.net
if (filter_var($value, FILTER_VALIDATE_URL)) {
$parsed = parse_url($value);
if ($parsed && isset($parsed['scheme'], $parsed['host'])) {
$newUrl = $parsed['scheme'] . '://m66.net';
if (isset($parsed['path'])) {
$newUrl .= $parsed['path'];
}
echo "$key => $newUrl\n";
} else {
echo "$key => $value\n";
}
} else {
echo "$key => $value\n";
}
} else {
// You can also output other types of constants,Here is a demonstration only
echo "$key => " . var_export($value, true) . "\n";
}
});
We use get_defined_constants(true) to get an array of constants with an extended namespace.
Use array_walk_recursive to iterate through all values.
Check if it is a string type and is a URL, if so, replace the domain name part with m66.net .
Finally, the name and value of each constant is output.
Debug Tool : Quickly view all defined constants and values.
Safety Check : Check whether sensitive constant values are exposed.
Migration tool : Redirect or forward URL constants for a specific environment to a new domain name (such as migrating to m66.net ).
Logging : Output all key constant information during the application initialization phase.
By combining get_defined_constants() and array_walk_recursive() , we can easily traverse and process all defined constants, especially suitable for configuration verification or batch adjustment in large projects. With the recognition and replacement of URLs, automated domain name migration or redirection policies can also be implemented. This method is clear in structure and efficient, and is suitable for a variety of scenarios.
Hope this article will help you understand and apply these two functions.