ReflectionClass不断返回的className不存在

问题描述 投票:0回答:1

[要做的是使用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

php reflection php-7 php-7.2
1个回答
0
投票

您尝试过这个吗?

$controllers = new \ReflectionClass("$classController")
© www.soinside.com 2019 - 2024. All rights reserved.