Search code examples
phparchitectureobjectclass-designoutline

Create a PHP Class Outline


I am looking for a function or class that can effectively outline a class:

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
  }

}

Is there a function or library in existence that can give me some kind of access to the information about this class, or even the file.

ex.

get_file_outline('fileWithAboveClass.php');

or

get_class_outline('MyClass');

Does anybody know of either, or know a way of easily writing this?


Solution

  • Take a look at the PHP Reflection 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();
    

    Note that for the comment extraction to work, they have to be formatted like PHPDoc / Doxygen comments, and begin with an opening /**