1

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]

)
4
  • Regarding my edit; it's still infeasible. Microtime tested it against the Zend_Controller_Front class file, (1007 lines, 3497 tokens) and it takes 0.010729945898056 on average (1000 iterations on my personal dev box) Commented Jun 11, 2011 at 7:44
  • did you get that right ? was wondering if you found a better way. Commented Nov 3, 2012 at 14:35
  • @dader No, a straight text parse of a given file may yield better performance but then you need to make sense of the results. Commented Nov 13, 2012 at 18:27
  • ok, I would propose this one, it may or may not be feasible. Using a recursive descent parser you could write a simple grammar that shall only distinguish between "mixed" code ( eventually up to the detail of class / method declaration, or statement ) and comments. Since you are not looking for an exhaustive parse tree, the grammar will stay simple. Then, you'll have a list of tokens that makes sense. Of course, it still uses the messy file_get_contents(FILE) approach... But I can't get rid of that, since everything in here is based on source code, not PHP logical structure. Commented Nov 14, 2012 at 17:31

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks C.Evenhuis; Well, that's unfortunate. I was curious because in my reading I heard about the PHP XP Framework supporting it, but after digging, I learned they had implemented their own reflection classes, which presumably adds support for it. docs.xp-framework.net/xml/doc?core/reflection
5

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 !

2 Comments

Thanks dader51; That's great, except that its using the messy file_get_contents(__FILE__) approach for self reflection, and would be difficult to associate a given comment with a definition (class, function, etc.)
Yes I know for the file_get_contents issue, moreover using the tokenizer for this purpose will bring you to the field of parsing, which is in fact a whole country...
2

AFAIK, for a comment to become documentation it is needed to start with /** not even with standard multi-line comment.

Comments

2

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.

Comments

2

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

1 Comment

Thanks Wessel Kranenborg; Darn. I had seen that mentioned elsewhere, I just didn't know if there was unmentioned support for non-doc-block comments. PHP should consolidate the user-comments on class methods when the comments are regarding an extended method, since getDocComment appears 3 times. I understand it's context, but still.

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.