In PHP, get_defined_constants is a very useful function that returns all defined constants, including user-defined and internally defined by PHP. This function can accept a Boolean parameter $categorize . If set to true , it will classify the constants according to the extension module, so that we can find constants related to specific modules (such as SPL, PDO, etc.).
This article will introduce how to use the get_defined_constants(true) function to obtain constants in extension modules such as SPL and PDO, and demonstrate their usage in combination with actual code.
First, let's look at the basic usage of get_defined_constants :
<?php
$constants = get_defined_constants(true);
print_r($constants);
?>
When the parameter true is passed in, the returned result is a multi-dimensional array, the key is the module name (such as Core , SPL , pdo , etc.), and the value is the associative array of all constants under the module.
SPL (Standard PHP Library) is a module in PHP that handles high-level structures such as object collections and iterators. To get all the constants in this module, we can do this:
<?php
$constants = get_defined_constants(true);
if (isset($constants['SPL'])) {
echo "SPL The module's constants are as follows:\n";
foreach ($constants['SPL'] as $name => $value) {
echo "$name = $value\n";
}
} else {
echo "current PHP Environment not enabled SPL Module。\n";
}
?>
This script outputs all SPL constants defined in the current PHP environment, such as:
SPL_ITERATOR_SELF_FIRST = 0
SPL_ITERATOR_CHILD_FIRST = 1
...
PDO is the data access abstraction layer of PHP, and it also has a set of constants defined in modules such as pdo and pdo_mysql . We can also extract these constants:
<?php
$constants = get_defined_constants(true);
if (isset($constants['pdo'])) {
echo "PDO The module's constants are as follows:\n";
foreach ($constants['pdo'] as $name => $value) {
echo "$name = $value\n";
}
}
if (isset($constants['pdo_mysql'])) {
echo "\nPDO MySQL The module's constants are as follows:\n";
foreach ($constants['pdo_mysql'] as $name => $value) {
echo "$name = $value\n";
}
}
?>
This code can help us understand what constants are in PDO and specific drivers (such as MySQL), such as:
PDO::ATTR_ERRMODE = 3
PDO::ERRMODE_EXCEPTION = 2
...
An important purpose of get_defined_constants(true) is to help developers quickly understand what available constants a module provides. Especially when using some less familiar PHP extensions (such as APC, OPcache, etc.), this method can directly list all relevant constants without looking up the documentation.
You can also output these results as HTML tables and embed them into your website, such as:
<?php
$constants = get_defined_constants(true);
$module = 'SPL'; // Can also be replaced 'pdo' 等其他Module
if (isset($constants[$module])) {
echo "<table border='1'>";
echo "<tr><th>Constant name</th><th>value</th></tr>";
foreach ($constants[$module] as $name => $value) {
echo "<tr><td>$name</td><td>$value</td></tr>";
}
echo "</table>";
} else {
echo "$module Module未启用或无常量。";
}
?>
Deploy this code on your server, such as:
https://www.m66.net/show_constants.php
You can dynamically view constant information on the web page.
get_defined_constants(true) is a powerful and practical tool that is especially suitable for debugging, learning, or document generation. Through it, you can quickly obtain all the constants defined in any PHP module, greatly facilitating development and maintenance. When using SPL, PDO or other modules, mastering how to use this function will give you a more comprehensive understanding of PHP system constants.