JD Isaacks
JD Isaacks

Reputation: 57974

Programmatically get class methods and properties?

OK first, given a file, say somefile.php is there a way to search that file for a Class?

Then, once you have the class, is there a way to get all the public properties and method signatures?

I am trying to create a way to document PHP classes on the fly.

Upvotes: 1

Views: 374

Answers (2)

Bill Karwin
Bill Karwin

Reputation: 562260

<?php

include("somefile.php");

if (class_exists("MyClass")) {
 $myclass = new ReflectionClass("MyClass");

 // getMethods() returns an array of ReflectionMethod objects
 $methods = $myclass->getMethods();

 foreach ($methods as $method) {
    print $method->getName() . "():\n";

    // getParameters() returns an array of ReflectionParameter objects
    $parameters = $method->getParameters();
    foreach ($parameters as $parm) {
      print " " . $parm . "\n";
    }
 }
}

Upvotes: 2

Related Questions