In PHP programming, it is often necessary to know which functions are already defined in the current script, especially when working on large projects. The get_defined_functions function provides a convenient way to do this. It is a built-in PHP function that allows you to retrieve all functions defined in the current script, including both system-built and user-defined functions.
The get_defined_functions function returns all functions currently defined in the PHP environment. The return value is an array containing the names of the functions. This function is especially useful for debugging and checking the list of functions included in the current script.
Function prototype:
<span><span><span class="hljs-title function_ invoke__">get_defined_functions</span></span><span>();
</span></span>
It takes no parameters and returns a multidimensional array. The array consists of two parts:
internal: A list of built-in system functions.
user: A list of user-defined functions.
This function returns an associative array with two keys:
internal: An array of built-in system functions.
user: An array of user-defined functions.
For example, here is a simple example demonstrating how to use the get_defined_functions function and view the returned result.
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Define a user-defined function</span></span><span>
</span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span> </span><span><span class="hljs-title">myCustomFunction</span></span><span>(</span><span><span class="hljs-params"></span></span><span>) {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Hello, world!"</span></span><span>;
}
<p></span>// Get all defined functions<br>
$functions = get_defined_functions();</p>
<p>// Print user-defined functions<br>
echo "User-defined functions:\n";<br>
print_r($functions['user']);</p>
<p>// Print built-in system functions<br>
echo "Built-in system functions:\n";<br>
print_r($functions['internal']);<br>
?><br>
</span>
<span><span>User-defined functions:
Array
(
[0] => myCustomFunction
)
<p>Built-in system functions:<br>
Array<br>
(<br>
[0] => abs<br>
[1] => acos<br>
[2] => acosh<br>
...<br>
)<br>
</span></span>
As shown above, the get_defined_functions function returns all defined functions, listing system functions and user-defined functions separately.
During development, you may need to debug a large PHP project. In this case, you can use get_defined_functions to check for duplicate function names or function conflicts.
By listing all defined functions, developers can generate documentation for the project's functions, helping team members understand all the functionalities used in the project.
If your application supports dynamically loading plugins or modules, you can use get_defined_functions to check whether the current module contains necessary functions. If the plugin module loads correctly, its functions should appear in the user-defined functions list.
get_defined_functions returns functions defined in the current PHP environment and does not include functions that have not yet been executed or loaded.
If you load external files or classes before calling get_defined_functions, functions from those files will also be included in the returned results.
get_defined_functions only lists defined functions and does not include classes or other PHP resources that have not been loaded.
get_defined_functions is a simple yet effective tool that helps developers view all functions in the current environment. Whether for debugging, generating documentation, or building plugin systems, it provides valuable information. This allows PHP developers to better manage and organize their code, ensuring the maintainability and extensibility of their projects.