Reputation: 20602
I've recently become familiar with Reflection
, and have been experimenting with it, especially getDocComment()
, however it appears that it only supports /** */
comment blocks.
/** foobar */
class MyClass{}
$refl = new ReflectionClass('MyClass');
// produces /** foobar */
echo $refl->getDocComment();
-Versus-
# foobar
class MyClass{}
$refl = new ReflectionClass('MyClass');
// produces nothing
echo $refl->getDocComment();
Is it not possible to capture this without resorting to any sort of file_get_contents(__FILE__)
nonsense?
As per dader51's answer, I suppose my best approach would be something along these lines:
// random comment
#[annotation]
/**
* another comment with a # hash
*/
#[another annotation]
$annotations
= array_filter(token_get_all(file_get_contents(__FILE__)), function(&$token){
return (($token[0] == T_COMMENT) && ($token = strstr($token[1], '#')));
});
print_r($annotations);
Outputs:
Array
(
[4] => #[annotation]
[8] => #[another annotation]
)
Upvotes: 1
Views: 1877
Reputation: 1440
As you can read here in the first User Contributed Note:
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.
So only /** */
blocks are given by this method.
I don't know any other method with php to get the other comments as using file_get_contents
and filter the comments with e.g. a regex
Upvotes: 2
Reputation: 1314
I was trying to do just a you a few days ago, and here is my trick. You can just use the php internal tokenizer ( http://www.php.net/manual/en/function.token-get-all.php ) , and then walk the array returned to select only the comments, here is a sample code :
$a = token_get_all(file_get_contents('path/to/your/file.php'));
print_r($a); // display an array of all tokens found in the file file.php
Here is a list of all tokens php recognize : http://www.php.net/manual/en/tokens.php
And the comment you will get by this method include ( from php.net site ) :
T_COMMENT : // or #, and /* */ in PHP 5
Hope it helps !
Upvotes: 4
Reputation: 11354
A doc comment as the name implies, is a documentation comment, not a standard comment, otherwise when you are grabbing comments for apps such as doxygen it will try to document any commented code from testing/debuggung, etc, which often gets left behind and is not important to the user of the API.
Upvotes: 2
Reputation: 26446
DocComments distinguish themselves by saying something about how your classes are to be used, compared to regular comments that could assist a developer in reading the code. That's also why the method isn't called getComment()
instead.
Of course it's all text parsing, and someone just made a choice in docComments always being these multiline comments, but that choice has apparently been made, and reading regular comments is not something in the reflection category.
Upvotes: 5
Reputation: 14603
AFAIK, for a comment to become documentation it is needed to start with /** not even with standard multi-line comment.
Upvotes: 2