views:

87

answers:

1

Are there any tools that can list the names of classes used by a PHP file?

For example, if I ran it on this file:

<?
class Test {
  public function __construct(Obj1 $x) {
    $y = new Obj2();
    $str = "Obj3";
    $z = new $str();
  }
}
?>

it would report "Obj1" and "Obj2". If it were really smart it might report "Obj3" as well, but that's not essential.

I'm trying to package up some code, and I want some help making sure that I didn't miss any dependencies.

There's something called PHP_Depend, which can graph the number of dependencies, but can't report what they are.

UPDATE: I didn't find a real solution, but I figured out something close enough for my purposes. You can tokenize a file, and search for all T_STRING tokens. This will give you all class names mentioned in the file. It will also give you other things, like function names and constants. But if your class names are easy to distinguish (e.g. they have initial caps), then this shouldn't be a problem.

$contents = file_get_contents($path);
$tokens = token_get_all($contents);
foreach ($tokens as $token) {
  if (is_array($token) && $token[0] === T_STRING) {
    echo $token[1]."\n";
  }
}
+1  A: 

There's PHPXref, which is a PHP cross referencing document generator.

wimvds