Skip to content
Advertisement

Automatically create documentation by reading function comments (PHP)

Let’s say I have:

class myclass {

    /*
     * This function, bla bla bla
     */
    function myclass()
    {
        return(true);
    }

    /*
     * This function, bla bla bla
     */
    function myfunc1()
    {
        return(true);
    }

    /*
     * This function, bla bla bla
     */
    function myfunc2()
    {
        return(true);
    }
}

By using get_class_methods(new myclass()); I can get the classes.

Now my question is this: can I read the comments from the class function into a string? So I can create auto generated documentation.

Advertisement

Answer

You can’t get access to comments from PHP if the target file is included with include, include_once, require or require_once because php-parser strips all comments out of code.

If you need to do it and don’t want to use PHPDocumentor or Doxygen, but want to do it yourself, you need to read the target file using file_get_contents or any other reading method and parse this code yourself using reguar expressions of other method of your choise(for example using this library – https://github.com/nikic/PHP-Parser). And parsing the code yourself you can get all information you need from comments.

But it is not so easy task so my advice is to use PHPDocumentor 😛

[EDIT 2021-07-08] Since PHP 5 you can do this pretty easily using ReflectionClass::getDocComment

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement