PHP,解析特定名称空间的所有类,并列出这些类的所有方法

时间:2018-10-17 07:50:29

标签: php class oop

我在特定目录中有一些类(例如src / faa / foo),所有这些类都具有相同的命名空间(App \ faa \ foo)。

我正在寻找一种从php脚本中列出这些类的所有方法的正确方法。

我想做这样的事情:

// list all class of this specific directory
$classes = get_all_class_by_directory_location('src/faa/foo');
// or
$classes = get_all_class_by_namespace('App\foo\faa');
    // but that means I must include theses classes to my script isn't it ? I think it's an ugly way because I only need print methods name, I don't need use them in this script 

foreach($classes as $class){
    print(get_methods($class));
}

什么是我想要做的最好方法?是否存在为此目的维护的社区php软件包?

我的项目遵循psr-4约定,也许此信息有用。

1 个答案:

答案 0 :(得分:1)

<?php

foreach (glob('src/faa/foo/*.php') as $file)
{
    require_once $file;

    // get the file name of the current file without the extension
    // which is essentially the class name
    $class = basename($file, '.php');

    if (class_exists($class))
    {
        $obj = new $class;
        foreach(get_class_methods($obj) as $method)
        {
          echo $method . '\n';
        }
    }
}

来自Create instances of all classes in a directory with PHP,然后添加了get_class_methods用法。

相关问题