4

I need to get list of functions with their contents (not only the function name) from php file. I tried to use regex but it has lots of limitations. it doesn't parse all types of functions. for example it fails if the function has if and for loop statements.

in details: I have around 100 include files. each file has number of declared functions. some files has functions duplicated in other files. so what I want is to get list of all functions from specific file then put this list inside an array then I will be using array unique in order to remove the duplicates. I read about tokenizer but I really have no idea how to make it grab the declared function with its data. all I have is this:

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;
}

unfortunately, this function lists only the function names.. what I need is to list the whole declared function with its structure.. so any ideas?

thanks in advance..

2
  • Do you want the function body, e.g. when you find function foo() {return 'foo';} you want it to return return 'foo;'. Please give an example of the expected results. And also provide some more details about why you would need that in the first place. Commented Apr 19, 2010 at 10:33
  • I'm working on script called "includer". this used to parse php files contain "include" statements. so we read the path of inclusions and get the data using file_get_contents. and put them back to the main file. I have about 100 include files. each one of these files has some functions. some of these functions have been re-declared in other files. so what I want is to get the list of all functions from all of these files contains these functions then I need to put them inside array and lastly use array_unique to remove the duplicates. if you have better solution please enlighten me.. thanks, Commented Apr 20, 2010 at 10:42

2 Answers 2

3

If you got the function names from your get_defined_functions, consider using Reflection API for the remaining work.

Example:

include 'file-with-functions.php';
$reflector = new ReflectionFunction('foo'); // foo() being a valid function
$body = array_slice(
    file($reflector->getFileName()), // read in the file containing foo()
    $reflector->getStartLine(), // start to extract where foo() begins
    $reflector->getEndLine() - $reflector->getStartLine()); // offset

echo implode($body);

Like @nunthrey suggested, you can also use Zend_Reflection to get both: the functions in a file and their content. Example with Zend_Reflection:

$reflector = new Zend_Reflection_File('file-with-functions.php');
foreach($reflector->getFunctions() as $fn) {
    $function = new Zend_Reflection_Function($fn->name);
    echo $function->getContents();
}
Sign up to request clarification or add additional context in comments.

5 Comments

hi, thanks for the replies really appreciated.. for first example you added. I cannot use include because the file I have contains duplicated functions. so when I include it to the main script I get duplicated declared function errors. for second example, I always get this error: "Fatal error: Class 'Zend_Reflection_File' not found" I'm using php v5.2.6 do I need to add extensions to make this work? thanks,
@ermac2014 Zend_Reflection is a component of framework.zend.com - you don't need an extension to use it, but you need to download the framework.
@ermac2014 should be right, yes. Why? Did it you it try it and it didn't work?
doesn't work I told you before... I get this error: "Fatal error: Class 'Zend_Reflection_File' not found" I need an extension to make it work. which I have no idea which one
You don't need an extension. All you have to do is download Zend Framework. The error you give sounds much more like PHP cannot find the path to the class, which is in Zend/Reflection/File.php.
1

Try Zend_Reflection http://framework.zend.com/manual/en/zend.reflection.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.