Speed up development with full-stack environments for every branch.

Learn More

How to create documentation using Reflection [PHP]

Forked from Dynamically invoking methods that a class has using Reflection.

32 Runs 116 Views 40 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/*
 * PHP Reflection - How to create documentation using Reflection
 */


//the class name to be documentated    
$className = "testClass";

//paste the name class with the extension .inc
$classNameExtension = $className . ".php";

//include the $classNameExtension class 
include($classNameExtension);

//get a ReflectionClass for the $className class
$reflection = new ReflectionClass($className);

//prepare the output
$display = '<table border="1">';

//get general information about the class
$display.="<html>\n<body>\n<table" .
        " style=\"table-layout:fixed;\" " .
        "cellpadding=\"5\" cellspacing=\"2\"" .
        " bgcolor=\"#D2D2D2\" border=\"1\"" .
        " bordercolor=\"#FFFFFF\">\n\t<tr><td" .
        " colspan=\"5\" align=\"center\"><font" .
        " face=\"arial\" size=\"6\"" .
        " color=\"purple\"> PHP Documentation" .
        " </font></td></tr>\n";

$display.="\t<tr><td align=\"center\"" .
        " colspan=\"0\"><font face=\"arial\"" .
        " size=\"4\" color=\"purple\">";
$display.=$reflection->getName();
$display.="</font></td><td " .
        "align=\"center\" colspan=\"4\">" .
        "<font face=\"arial\" size=\"2\"" .
        " color=\"black\">\n<i>";
$display.=$reflection->getDocComment();

//get information about the interfaces
$classInterfaces = $reflection->getInterfaces();

if ($classInterfaces != null) {
    $display.="\n</i>\t</td></tr>\n\t<tr>" .
            "<td align=\"center\" colspan=\"0\">" .
            "<font face=\"arial\" size=\"2\"" .
            " color=\"purple\">Implemented" .
            " interfaces:</td><td align=\"center\"" .
            " colspan=\"0\"><font face=\"arial\"" .
            " size=\"2\" color=\"black\"><b>Name</b>" .
            "</font></td><td align=\"center\"" .
            " colspan=\"3\"><font face=\"arial\"" .
            " size=\"2\" color=\"black\"><b>" .
            "Description</b></font></td></tr>\n";
    foreach ($classInterfaces as $in) {
        $display.="\t<tr><td></td><td align=" .
                "\"center\">";
        $display.=$in->getName();
        $display.="</td><td align=\"center\"" .
                " colspan=\"3\"><i>\n";
        $display.=$in->getDocComment();
        $display.="</i></td></tr>\n";
    }
}

//get the superclass information
$superclass = $reflection->getParentClass();
if ($superclass != null) {
    $display.="\t<tr><td align=\"center\"" .
            "colspan=\"0\"><font face=\"arial\"" .
            " size=\"2\" color=\"purple\">" .
            "The superclass is:</td><td" .
            " align=\"center\" colspan=\"4\">" .
            "<font face=\"arial\" size=\"2\"" .
            " color=\"black\"><b>\n";
    $display.=$superclass->getName();
    $display.="</font></td></tr>\n";
}

//get information about the constants
$constants = $reflection->getConstants();
if ($constants != null) {
    $constantsNumber = count($constants);
    $display.="\t<tr><td align=\"center\"" .
            " colspan=\"0\"><font face=\"arial\"" .
            " size=\"2\" color=\"purple\">" .
            "Constants:</td><td align=\"center\"" .
            " colspan=\"0\"><font face=\"arial\"" .
            " size=\"2\" color=\"black\"><b>Name</b>" .
            "</font></td><td align=\"center\"" .
            " colspan=\"3\"><font face=\"arial\"" .
            " size=\"2\" color=\"black\"><b>Value</b>" .
            "</font></td></tr>\n";
    foreach ($constants as $keys => $value) {
        $display.="\t<tr><td></td><td" .
                " align=\"center\">";
        $display.=$keys;
        $display.="</td><td align=\"center\"" .
                " colspan=\"3\">";
        $display.=$value;
        $display.="</td></tr>\n";
    }
}

//get information about properties
$properties = $reflection->getProperties();
if ($properties != null) {
    $display.="\t<tr><td align=\"center\"" .
            " colspan=\"0\"><font face=\"arial\"" .
            " size=\"2\" color=\"purple\">" .
            "Properties:</td><td align=\"center\"" .
            " colspan=\"0\"><font face=\"arial\"" .
            " size=\"2\" color=\"black\"><b>Name</b>" .
            "</font></td><td align=\"center\"" .
            " colspan=\"3\"><font face=\"arial\"" .
            " size=\"2\" color=\"black\"><b>" .
            "Modifiers</b></font></td></tr>\n";
    foreach ($properties as $in) {
        $display.="\t<tr><td></td><td" .
                " align=\"center\" colspan=\"0\">";
        $display.=$in->getName();
        $display.="</td><td align=\"center\"" .
                " colspan=\"3\">";

        if ($in->isPublic()) {
            $display.="[public]";
        }
        if ($in->isPrivate()) {
            $display.="[private]";
        }
        if ($in->isProtected()) {
            $display.="[protected]";
        }
        if ($in->isStatic()) {
            $display.="[static]";
        }
        $display.="</td></tr>\n";
    }
}

//get information about methods
$methods = $reflection->getMethods();

if ($methods != null) {
    $display.="\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>" .
            "</td></tr>\n";
    foreach ($methods as $in) {
        $display.="\t<tr><td></td><td>";
        $display.=$in->getName();

        if ($in->isConstructor()) {
            $display.=" [c]";
        }

        $display.="</td><td>";
        if ($in->isPublic()) {
            $display.="[public]";
        }
        if ($in->isPrivate()) {
            $display.="[private]";
        }
        if ($in->isProtected()) {
            $display.="[protected]";
        }
        if ($in->isAbstract()) {
            $display.="[abstract]";
        }
        if ($in->isFinal()) {
            $display.="[final]";
        }
        if ($in->isStatic()) {
            $display.="[static]";
        }
        $display.="</td>";

        $parameters = $in->getParameters();
        if ($parameters != null) {
            $display.="<td align=\"center\">";
            $nr_parameters = count($parameters);
            foreach ($parameters as $out) {
                $display.="$";
                $display.=$out->getName();
                if ($out->isPassedByReference()) {
                    $display.="  [&]  ";
                }
                if ($out->allowsNull()) {
                    $display.="  [+]  ";
                }
            }
            $display.="</td>";
        } else {
            $display.="<td></td>";
        }

        $display.="<td align=\"center\"><i>";
        $display.=$in->getDocComment();
        $display.="</i>\n\t</td></tr>\n";
    }
}

$display.="<tr><td colspan=\"5\"><b>" .
        "<i>Legend: [+] - Parameter allows null" .
        " / [&] - Parameter passed by reference" .
        " / [c] - Constructor</i></b></td></tr>";

$display.="</table>\n</body>\n</html>";

echo $display;
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).