How to list the functions in a PHP file

When debugging your PHP code you often run into an issue where you’re trying to find the scope of what variables are available. Whether you’re taking over a project from someone, or you’re using a CMS or framework, being able to list in an array all the functions comes handy.

Here’s the simple PHP function to get all the functions available to you:

function myfunc() {
  echo 'Hello World!';
}

$list = get_defined_functions();

print_r($list);

This will output something like:

Array
(
    [internal] => Array
        (
            [1] => func_num_args
            [2] => func_get_arg
            [3] => func_get_args
            [4] => strlen
            [5] => strcmp
            [6] => strncmp
            ...
            [750] => bcscale
            [751] => bccomp
        )

    [user] => Array
        (
            [0] => myfunc
        )

)

That gives you the internal, php core functions, and user defined functions (the ones you or your CMS defines). Let’s take this a step further and make a function that gets only the user defined functions.

function get_functions() {
  $list = get_defined_functions();
  $user_list = array_flip($list['user']);
  return $user_list;
}

$list = get_functions();

print_r($list);

You can rename get_functions() to whatever you want it to be. I usually prefix my functions based on the specific CMS or framework I’m using.

Conclusion

You’ve just learned something pretty basic, but handy. Use with care.

 

Leave a comment

Find the Best SEO in Fort Lauderdale, Florida (Plantation SEO) | Drupal & Worpress Develpment