سؤال

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 :)

هل كانت مفيدة؟

المحلول

Try using proper phpDoc comments:

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

نصائح أخرى

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."

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top