RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Taking Advantage of the New Reflection API in PHP5 : Page 4

Discover how to use the Reflection API to extract information about PHP classes and execute class methods.

Example 2: Generating PHP Documentation with Reflection
One final example generates PHP class documentation using reflection—a task that requires deep class introspection, and thus uses many Reflection API methods.

First you need a simple HTML-based console that lets clients specify the name of the class to document. The console code could be as simple as this:

         <b>Provide the class name to be documentated: <br /><br /></b>
      <form method="get" action="generatingDocumentation.php">
        <input type="text" name="class">
        <input type="submit" value="Generate documentation">
        <input type="reset" value="Reset">
The generatingDocumentation.php code in Listing 2 functions as the main component of the documentation application.

The page first retrieves the class name submitted by the user through the console, adds a file extension, includes the class, and then opens a file where it will write the output documentation:

      //get the class name to be documentated    
      //paste the name class with the extension .inc
      //include the $classNameExtension class 
      //get a ReflectionClass for the $className class
      $reflection=new ReflectionClass($className);
      //prepare the output
Next it cycles through the various parts of the class, retrieving the interfaces the class supports, the name of its parent class (if any), constants, properties, and finally the methods, along with all their parameters. It writes all that information, including any description comments, into the documentation file. You can see the full code in Listing 2, but here's the section that retrieves and documents a class's methods and parameters:

   //get information about methods
      if($methods != null) 
        fwrite($hf,"\t<tr><td align=\"center\"".
        " colspan=\"0\"><font face=\"arial\"".
        " size=\"2\" color=\"purple\">Methods:".
        "</td><td align=\"center\" colspan=\"0\">".
        "<font face=\"arial\" size=\"2\"".
        " color=\"black\"><b>Name</b></td>".
        "<td align=\"center\" colspan=\"0\">".
        "<font face=\"arial\" size=\"2\"".
        " color=\"black\"><b>Modifiers</b>".
        "</td><td align=\"center\" colspan=\"0\">".
        "<font face=\"arial\" size=\"2\"".
        " color=\"black\"><b>Parameters</b>".
        "</td><td align=\"center\" colspan=\"0\">".
        "<font face=\"arial\" size=\"2\"".
        " color=\"black\"><b>Description</b>".
        foreach($methods as $in)
                 { fwrite($hf,"&nbsp;[c]"); }
         if ($in->isPublic())
                 { fwrite($hf,"[public]"); } 
         if ($in->isPrivate())
                 { fwrite($hf,"[private]"); }
         if ($in->isProtected())
                 { fwrite($hf,"[protected]"); } 
         if ($in->isAbstract())
                 { fwrite($hf,"[abstract]"); } 
         if ($in->isFinal())
                 { fwrite($hf,"[final]"); } 
         if ($in->isStatic())
                 { fwrite($hf,"[static]"); } 
         if($parameters != null)
            fwrite($hf,"<td align=\"center\">");
            foreach($parameters as $out)
Figure 3. PHP Documentation Output: The PHP Documentation Tool application emits documentation showing the interfaces, superclass, constants, properties, and methods.
fwrite($hf,"$"); fwrite($hf,$out->getName()); if($out->isPassedByReference()) { fwrite($hf,"&nbsp; [&] &nbsp;"); } if($out->allowsNull()) { fwrite($hf,"&nbsp; [+] &nbsp;"); } } fwrite($hf,"</td>"); }else { fwrite($hf,"<td></td>"); } fwrite($hf,"<td align=\"center\"><i>"); fwrite($hf,$in->getDocComment()); fwrite($hf,"</i>\n\t</td></tr>\n"); } }
For example, if you provide the test class in Listing 1 to the PHP Documentation Tool, it will output the information shown in Figure 3.

As you have seen, the new Reflection API provides a good implementation of reflection, and gives you the tools to create more complex applications, such as the PHP Documentation Tool. You'll find reflection particularly useful when you don't know which class or method you need to execute at design time.

Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies—XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date