tags:

views:

117

answers:

4

How to get list of functions that are declared in a php file

A: 

You just want a list of all the functions that are in your file?

vette982
That should be a comment, not an answer.
Andrew Moore
yes, i want a list of all the functions that are in my file
zumrasha
+3  A: 

You can use the get_defined_functions() function to get all defined function in your current script.

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

If you want to get the functions defined in another file, you can try using http://www.php.net/token_get_all like this:

$arr = token_get_all(file_get_contents('anotherfile.php'));

Then you can loop through to find function tokens with the symbols defined. The list of tokens can be found http://www.php.net/manual/en/tokens.php

thephpdeveloper
get_defined_functions() gets all my declared functions from all files, not from exacly one file!
zumrasha
updated. you might want to use `token_get_all()`. If you use includes and requires, the functions defined in the other files will also be defined in this file.
thephpdeveloper
+1  A: 

You can get a list of currently defined function by using get_defined_functions():

$arr = get_defined_functions();
var_dump($arr['user']);

Internal functions are at index internal while user-defined function are at index user.

Note that this will output all functions that were declared previous to that call. Which means that if you include() files with functions, those will be in the list as well. There is no way of separating functions per-file other than making sure that you do not include() any file prior to the call to get_defined_functions().


If you must have the a list of functions per file, the following will attempt to retrieve a list of functions by parsing the source.

function get_defined_functions_in_file($file) {
    $source = file_get_contents($file);
    $tokens = token_get_all($source);

    $functions = array();
    $nextStringIsFunc = false;
    $inClass = false;
    $bracesCount = 0;

    foreach($tokens as $token) {
        switch($token[0]) {
            case T_CLASS:
                $inClass = true;
                break;
            case T_FUNCTION:
                if(!$inClass) $nextStringIsFunc = true;
                break;

            case T_STRING:
                if($nextStringIsFunc) {
                    $nextStringIsFunc = false;
                    $functions[] = $token[1];
                }
                break;

            // Anonymous functions
            case '(':
            case ';':
                $nextStringIsFunc = false;
                break;

            // Exclude Classes
            case '{':
                if($inClass) $bracesCount++;
                break;

            case '}':
                if($inClass) {
                    $bracesCount--;
                    if($bracesCount === 0) $inClass = false;
                }
                break;
        }
    }

    return $functions;
}

Use at your own risk.

Andrew Moore
get_defined_functions() gets all my declared functions from all files, not from exacly one file!
zumrasha
@zumrasha: See my updated answer, which will give you a list of available functions per-file basis.
Andrew Moore
A: 

do include to the file and try this :

$functions = get_defined_functions();
print_r($functions['user']);
Haim Evgi