0

I am aware of PHP ReflectionClass to get doc comments from PHP scripts. Now I have come across these hundreds of old PHP scripts that are not using classes but only simple public functions. If I am right, ReflectionClass cannot be used here? My goal is to extract each doc comments. E.g. test1.php contains the following lines:

<?php
/**
 * some main comments here
 */
...some php lines here...

/**
 * function comment
 * Blah blah
 */
function foo()
{
 ...some php lines here...
}
...and more functions below each with comments

?>

So my desired ouput would be a table like:

--------------------------------------------------
| Filename  | FUNCTION | Comment                 |
--------------------------------------------------
| test1.php |          | some main comments here |
| test1.php | foo()    | function comment        |
|           |          | Blah Blah               |
tatskie
  • 405
  • 1
  • 7
  • 16

1 Answers1

1

You can use ReflectionFunction

$functions = get_defined_functions();

foreach ($functions['user'] as $function) {

    $reflection = new ReflectionFunction($function);

    var_dump($reflection->getName());
    var_dump($reflection->getDocComment());
}

The result would be (in case you have only the foo function defined):

string(3) "foo"
string(43) "/**
 * function comment
 * Blah blah
 */"
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • Matei, How or where to specify the file test1.php? In my case, I will have hundreds of them say `test1.php` to `test100.php`. How does ReflectionFunction know which PHP file it's currently working on? Pardon me, there is not much documentation I can google around. – tatskie Mar 24 '17 at 23:29
  • Matei, also, what about the comment "some main comments here" at the start of `test1.php`? There are comments similar to this every start of a PHP file. – tatskie Mar 24 '17 at 23:41
  • I have to mark your answer correct and have to give credits also to Joachim from http://stackoverflow.com/questions/2197851/function-list-of-php-file. – tatskie Mar 25 '17 at 05:38