[要做的是使用scandir()
扫描目录并在其中获取所有文件,然后使用ReflectionClass
读取或访问该类。
我能够获取目录中的所有文件并将其分配给数组,但由于其不断返回而无法使用properties
读取或访问methods
和\ReflectionClass
Class App \ Controllers \ AtController不存在
这是我到目前为止的代码
// The directory to scan and assign it to a variable
$classControllers = scandir($cur_dir . '/app/Controllers');
// Change to the ReflectionClass to access
chdir($cur_dir . '/app/Controllers/');
// Dump the $classControllers to see if it truly scan the directory
var_dump($classControllers);
$control = '';
// Loop over the $classControllers
foreach($classControllers as $classController){
if($classController != "." && $classController != "..")
{
$classController = str_ireplace(".php", "", $classController);
echo $classController . PHP_EOL;
// Use ReflectionClass to read the class name, methods and properties of each class.
$controllers = new \ReflectionClass("App\\Controllers\\$classController")#App\Controllers is the namespace of the files in the directory;
$controller = $controllers->getName();
$control = substr($controller, 12);
$control = ucfirst($control);
$routeScript .= "\t$control" . "," . PHP_EOL;
}
}
NB:new \ReflectionClass("App\\Controllers\\$classController"); #App\Controllers is the namespace of the files in the directory
您尝试过这个吗?
$controllers = new \ReflectionClass("$classController")