1

I am trying to find a best way to get all the private, public, static, protected methods in a file.... what would be the best way to do it. currently when I do file_get_contents it dumps the whole file but I need some kind of regex that will give me methods only

$filecontent = file_get_contents($fn->getPath()."/".$fn->getFilename());

I am not sure if I can use this

preg_match("/private function | protected function | public function | public static function/") etc etc

if there is a better way I would like to know about that as well

hakre
  • 193,403
  • 52
  • 435
  • 836
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221

1 Answers1

3

Use reflection, assuming your path is PSR-0 you can do something along the lines:

<?php

$document_root = "/document/root";

$file = "{$document_root}/PSR/Compatible/Path/ClassName.php";

$class = str_replace(
  array($document_root, DIRECTORY_SEPARATOR, ".php"),
  array("", "\\", ""),
  $file
);

$reflector = new \ReflectionClass($class);

var_dump($reflector->getMethods());

?>

Hope this helps.

Fleshgrinder
  • 15,703
  • 4
  • 47
  • 56