2

I want to fetch a method's comments,take below method for example:

/**
* Returns the regex to extract all inputs from a file.
* @param string The class name to search for.
* @return string The regex.
*/
public function method($param)
{
  //...
}

the result should be

Returns the regex to extract all inputs from a file.
@param string The class name to search for.
@return string The regex.

the way I find is use a function like file_get_content to get file content -> filter the method I want -> fetch the comment use regexp

it seems a bit complicated , is there any convenient way to archive this?

4 Answers 4

6

actually you can get a method's doc comments with getDocComment

$ref=new ReflectionMethod('className', 'methodName');

echo $ref->getDocComment();
Sign up to request clarification or add additional context in comments.

2 Comments

This is correct, however it would be nice if you have mentioned that doc comment should be wrapped in /** */ to be retrieved and then if there are multiple comments on a method only closest comment will be returned and also the returned string contains the comment stars too and a link to ReflectionClass::getDocComment for further reading.
Also note that, at the time of asking this question method you have mentioned wasn't exist.
4

If you want to use the comment in PHP for something check out getDocComment in php's reflection api

1 Comment

after a deep look , it isn't what I need . getDocComment just fetch class's comment , not method's. so I write a small script to do this gist.github.com/329113
1

PHP Doc. Like Java Doc.

Comments

0

For a method dump I use this little function I composed. It fetches all methods from provided class that are public(and thus of use to you).

I personally use a dump() method to nicely format the outputted array of method names and descriptions, but that's not needed if you wish to use it for something else :-)

function getDocumentation($inspectclass) {
    /** Get a list of all methods */
    $methods = get_class_methods($inspectclass);
    /** Get the class name */
    $class =get_class($inspectclass);
    $arr = [];
    foreach($methods as $method) {
        $ref=new ReflectionMethod( $class, $method);
        /** No use getting private methods */
        if($ref->isPublic()) {
            $arr[$method] = $ref->getDocComment();
        }
    }
    /** dump is a formatting function I use, feel free to use your own */
    return dump($arr);
}
echo getDocumentation($this);

Comments

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.