PHP get_defined_functions() Function

What is PHP get_defined_functions() Function?

If you want to get all the defined functions in your script be them built-in functions (internal) and user-defined functions, call get_defined_vars() function. Check example 1.

Syntax:

get_defined_functions(exclude_disabled)

Parameters:

The function has one parameter which is optional-

exclude_disabled (Optional): it indicates whether the disabled built-in functions should be excluded or not. It can take two values-

  • TRUE – This value excludes specified built-in functions to display. To disable a function mention it in the disable_functions in the php.ini file like disable_functions = dir, system, exec). Check example 4.
  • FALSE – This value doesn’t exclude specified built-in functions to display.

Return Values:

The function returns a multidimensional array of the following two types of defined arrays-

  • Built-in (internal) arrays: it includes all the built-in functions. Check example 2.
  • User-defined arrays: It includes all the user-defined functions. Check example 3.

Examples:

Example 1:

<pre>
<?php
function my_function1(){}
function my_function2(){}
print_r(get_defined_functions());
?>
</pre>

Output:

Array
(
    [internal] => Array
        (
            [0] => zend_version
            [1] => func_num_args
            [2] => func_get_arg
        …………
        }
    [user] => Array
        (
            [0] => my_function1
            [1] => my_function2
        )
)

Explanation:

The get_defined_functions() function displays both internal (built-in) and user-defined functions.

Example 2:

<pre>
<?php
function my_function3(){}
function my_function4(){}
print_r(get_defined_functions()["internal"]);
?>
</pre>

Output:

Array
(
    [internal] => Array
        (
            [0] => zend_version
            [1] => func_num_args
            [2] => func_get_arg
        …………
        )
)

Explanation:

The get_defined_functions()[“internal”] function only displays built-in (internal) functions.

Example 3:

<pre>
<?php
function my_function5(){}
function my_function6(){}
print_r(get_defined_functions()["user"]);
?>
</pre>

Output:

Array
(
    [0] => my_function5
    [1] => my_function6
)

Explanation:

The get_defined_functions()[“user”] function only displays user-defined functions.

Example 4:

<pre>
<?php
function my_function7(){}
function my_function8(){}
print_r(get_defined_functions(TRUE));
?>
</pre>

Output:

Array
(
    [internal] => Array
        (
            [0] => zend_version
            [1] => func_num_args
            [2] => func_get_arg
        …………
        )
)

Explanation:

The get_defined_functions(TRUE) doesn’t return the specified built-in (internal) functions mentioned in the php.ini file.

Practical Usages of get_defined_functions() Function:

For debugging purpose, you may use get_defined_functions() function to get the full list of all the defined functions.

PHP Version Support:

PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8

Summary: PHP get_defined_functions() Function

When you need to know either al the built-in functions or all your defined functions or both, you can use get_defined_functions() function. It is a built-in PHP function handling type functions.

Reference:

https://www.php.net/manual/en/function.get-defined-functions.php