0

I need to get a comment of the particular function in php class, for example:

/* Some commets for a class */
class Foo extends Bar {
    function __construct() {}

    // a single line comment to the function foo()
    function foo() {}

    /* a multi-line comment
    to the function bar() */
    public static function bar() {}

}

Yes i know, that could be easily done with ReflectionMethod->getDocComment(), but it does not work for me because i use eAccelerator and it cuts all comments from the code, so that getDocComment always returns FALSE.

I don't want to recompile eAccelerator too :)

I need a function like this:

function get_function_comment($class_contents, $function_name) {}

so that i will return a function's comment, $class_contents is a variable wich stores the class content as in the example above.

I tried to do it myself but i can't create a proper regexp..

Please help me :)

4
  • 2
    I don't think a single regular expression is going to cut it. Commented Oct 19, 2011 at 15:40
  • You're not going to be able to write a regex that implements the entirety of PHP's syntax parser... Commented Oct 19, 2011 at 15:41
  • 1
    If this is possible at all without a lot of messing around, Tokenizer will be the way to do it. Commented Oct 19, 2011 at 15:43
  • getDocComment only returns specifically formatted comments that the parser deems as T_DOC_COMMENT's. It doesn't return other comments. Commented Oct 19, 2011 at 15:49

2 Answers 2

1

Oh man, I almost feel dirty for writing this regex, but this might do the trick (haven't tested it, so don't take my word).

preg_match('#(//.*$|/\*.*\*/)\s*$[\s\w]*function\s+$function_name\b#Usmi', $class_contents, $result);

In theory, it works like:

  • Find either:
    • // and everything till the end of that line OR
    • /*, then everything until an */
  • Then eat all the whitespace till the end of line
  • Take any amount of whitespace or word characters on the next line until you hit "function", some whitespace, then the whole function name you want.

In practice: "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."

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

1 Comment

Actually this does not work when class has comments too. Like this: ` /** CMS users management */` ` class Adminusers extends Module {` ` }` So it returns the text from the beginning of class contents to required function name. But i need just function's comment :)
0

Try using proper phpDoc comments:

/**
 * Something explaining this
 *
 * @return string
 */
function foo(){  }

1 Comment

It does not matter what comments to use. eAccelerator will cut it anyway.

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.