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               |
2
  • did you try some doc generators? phpdox.de or phpdoc.org Commented Mar 24, 2017 at 17:31
  • Muescha, thanks. Not yet. I would appreciate if you can hint me further :) Commented Mar 25, 2017 at 0:22

1 Answer 1

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
 */"
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
I have to mark your answer correct and have to give credits also to Joachim from stackoverflow.com/questions/2197851/function-list-of-php-file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.