【问题标题】:Create a PHP Class Outline创建 PHP 类大纲
【发布时间】:2011-01-22 19:28:53
【问题描述】:

我正在寻找可以有效勾勒出一个类的函数或类:

class MyClass{

  /*
  * Perhaps include the function comments
  * in the function.
  */
  function mainFunction(){
    //Does Something
  } 

  function functionWithArgs($arg1,$arg2=false){
    //Does Something
    //The function I want will give e the arguments w/default values
  }

}

是否存在可以让我以某种方式访问​​有关此类甚至文件的信息的函数或库。

例如

get_file_outline('fileWithAboveClass.php');

get_class_outline('MyClass');

有没有人知道,或者知道如何轻松编写这个?

【问题讨论】:

    标签: php architecture object class-design outline


    【解决方案1】:

    看看PHPReflection API

    //use the ReflectionClass to find out about MyClass
    $classInfo = new ReflectionClass('MyClass'); 
    
    //then you can find out pretty much anything you want to know...
    $methods = $classInfo->getMethods(); 
    var_dump($methods);
    
    //you can even extract your comments, e.g.
    $comment=$classInfo->getMethod('mainFunction')->getDocComment();
    

    请注意,要使评论提取起作用,它们必须像 PHPDoc / Doxygen cmets 一样格式化,并以开头 /**

    【讨论】:

    • 这很有帮助,谢谢!有没有办法获取函数参数?
    • 是的,一旦你获得了方法的 ReflectionMethod 对象,你就可以获得各种信息,见php.net/manual/en/class.reflectionmethod.php
    • 是的,我刚刚看到了。谢谢一百万,你为我节省了大量时间:)
    【解决方案2】:

    还有一个命令行选项可用于检查函数和类。

    $ php --rc DateTime
    

    将为您提供有关 DateTime 类的所有详细信息,同时

    $ php --rf in_array
    

    会给你“in_array”函数的参数。

    如果您在编码时使用终端,则使用起来非常方便,而不是一直在 PHP 手册中查找;)

    【讨论】:

      猜你喜欢
      • 2021-10-28
      • 2023-03-08
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多