获取文件中的所有方法

时间:2013-10-24 21:37:42

标签: php regex lithium

我正在尝试找到一种最好的方法来获取文件中的所有私有,公共,静态和受保护的方法....这是最好的方法。目前,当我执行file_get_contents时,它会转储整个文件,但我需要一些只给我方法的正则表达式

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

我不确定我是否可以使用此

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

如果有更好的方式我也想知道

1 个答案:

答案 0 :(得分:3)

使用反射,假设你的路径是PSR-0,你可以做一些事情:

<?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());

?>

希望这有帮助。