Php 解析特定名称空间的所有类,并列出这些类的所有方法
我在特定目录中有一些类(例如src/faa/foo),所有这些类都有相同的名称空间(App\faa\foo) 我正在寻找一种合适的方法,从php脚本中列出这些类的所有方法 我想这样做:Php 解析特定名称空间的所有类,并列出这些类的所有方法,php,class,oop,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\
// 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惯例,也许这些信息是有用的
使用反射API:谢谢你,我看到了!
<?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';
}
}
}