PHP: Hypertext Preprocessor (original) (raw)
Found A Problem?
gabe at fijiwebdesign dot com ¶
10 years ago
`You can also get the docblock definitions for the defined methods of a class as such:
getDocComment()// to get the Method DocBlock $reflector->getMethod('fn')->getDocComment();?>`
14 years ago
`According to what I can find in the PHP (5.3.2) source code, getDocComment will return the doc comment as the parser found it.
The doc comment (T_DOC_COMMENT) must begin with a /** - that's two asterisks, not one. The comment continues until the first /. A normal multi-line comment /...*/ (T_COMMENT) does not count as a doc comment.
The doc comment itself includes those five characters, so will get you what's inside. A call to trim() after is recommended.
`
joe dot scylla at gmail dot com ¶
15 years ago
If you're using a bytecode cache like eAccelerator this method will return FALSE even if there is a properly formatted Docblock. It looks like the information required by this method gets stripped out by the bytecode cache.
geoffreybans at gmail dot com ¶
9 years ago
`This code can help you get the contents of a docBlock in array format beginning with the @symbol and ignoring the (*) asterists.
class Home {
/**
*This method loads the homepage
*@param int $id The user id
*@throws \Exception If the user id doesn't exist
*@return void
*/
public function index( $id)
{
#...your code here
}
}
$object = new Home();
//get the comment string commentstring=(newReflectionClass(comment_string= (new ReflectionClass(commentstring=(newReflectionClass(object))->getMethod('index')->getdoccomment();
//define the regular expression pattern to use for string matching
$pattern = "#(@[a-zA-Z]+\s*[a-zA-Z0-9, ()_].*)#";
//perform the regular expression on the string provided
preg_match_all($pattern, commentstring,comment_string, commentstring,matches, PREG_PATTERN_ORDER);
echo "
"; print_r($matches);//this outputs
Array
(
[0] => Array
(
[0] => @param int $id The user id
[1] => @throws \Exception If the user id doesn't exist
[2] => @return void
)[1] => Array
(
[0] => @param int $id The user id
[1] => @throws \Exception If the user id doesn't exist
[2] => @return void
))
//you can then be able to access the particular string values by index
`
10 years ago
`Note that \ReflectionClass::getDocComment() ignores all other PHP code and all white-space between the last encountered T_DOC_COMMENT and the class/element definition.
The only exceptions appear to be T_NAMESPACE declarations and T_FUNCTION definitions.
getDocComment()); ?>yields, despite all the garbage in between:
string(28) "/**
- After namespace.
*/"
To sum up:
If there are multiple doc comments, the last encountered applies.
Removing the "After namespace." docblock yields FALSE.
(The namespace delimits the scope.)Uncommenting the function definition yields FALSE.
(The doc comment applies to the function instead.)Despite being an own language construct, the "const" constant declaration does not delimit the scope.
Any leading and trailing white-space before and after the T_DOC_COMMENT ("/**...*/") is ignored, but the entire string content within (including all white-space) is consumed literally/verbatim.
[PHP 5.4.29]
`
7 years ago
`Not only this method can find a document by a given method name of a class, like this
class Foo{
/**
- something describes this method
/
function bar(){
/* code here... */
}
}
$ref = new ReflectionClass('Foo');
print_r($ref->getMethod('bar')->getDocComment()); //will print out the method document as expected.
?>
But, also, if there is no method document in the defination, and the class extends from some other class, the program will automatically
try to find document from the parent class., like this: getMethod('bar')->getDocComment()); //will print out the method document as expected. ?>`