代码之家  ›  专栏  ›  技术社区  ›  limboy

获取PHP方法注释

  •  1
  • limboy  · 技术社区  · 16 年前

    我想获取一个方法的注释,以下面的方法为例:

    /**
    * Returns the regex to extract all inputs from a file.
    * @param string The class name to search for.
    * @return string The regex.
    */
    public function method($param)
    {
      //...
    }
    

    结果应该是

    Returns the regex to extract all inputs from a file.
    @param string The class name to search for.
    @return string The regex.
    

    我找到的方法是使用类似“文件获取内容”的函数获取文件内容->筛选我想要的方法->获取注释使用regexp

    这似乎有点复杂,有什么方便的方法来存档吗?

    4 回复  |  直到 11 年前
        1
  •  4
  •   rojoca    16 年前

    如果您想在PHP中使用注释,请查看 getDocComment 在PHP中 reflection api

        2
  •  1
  •   VDVLeon    16 年前

    PHP文档。像Java文档一样。

        3
  •  1
  •   Reza    11 年前

    实际上,您可以使用getDocComment获取方法的Doc注释

    $ref=new ReflectionMethod('className', 'methodName');
    
    echo $ref->getDocComment();
    
        4
  •  0
  •   Tschallacka    11 年前

    对于方法转储,我使用我编写的这个小函数。 它从提供的公共类(因此对您有用)中获取所有方法。

    我个人使用dump()方法来很好地格式化输出的方法名和描述数组,但如果您希望将其用于其他用途,则不需要这样做:—)

    function getDocumentation($inspectclass) {
        /** Get a list of all methods */
        $methods = get_class_methods($inspectclass);
        /** Get the class name */
        $class =get_class($inspectclass);
        $arr = [];
        foreach($methods as $method) {
            $ref=new ReflectionMethod( $class, $method);
            /** No use getting private methods */
            if($ref->isPublic()) {
                $arr[$method] = $ref->getDocComment();
            }
        }
        /** dump is a formatting function I use, feel free to use your own */
        return dump($arr);
    }
    echo getDocumentation($this);
    
    推荐文章