Determining what classes are defined in a PHP class file

后端 未结 8 1366
-上瘾入骨i
-上瘾入骨i 2020-11-27 14:56

Given that each PHP file in our project contains a single class definition, how can I determine what class or classes are defined within the file?

I know I could jus

8条回答
  •  半阙折子戏
    2020-11-27 15:04

    Or you could easily use AnnotationsParser from Nette\Reflection (installable using composer):

    use Nette\Reflection\AnnotationsParser;
    $classes = AnnotationsParser::parsePhp(file_get_contents($fileName));
    var_dump($classes);
    

    Output will be then something like this:

    array(1) {
      ["Your\Class\Name"] =>
      array(...) {
          // property => comment
      },
      ["Your\Class\Second"] =>
      array(...) {
          // property => comment
      },
    }
    

    The parsePhp() method basically does something similar as examples in other answers, but you don't have to declare nor test the parsing yourselves.

提交回复
热议问题