Search code examples
phpreflectionphp-7php-7.2

ReflectionClass keeps returning className does not exist


What am trying to do is use scandir() to scan a directory and get all the files there, then use the ReflectionClass to read or access the class.

I was able to get all the files in the directory and assigned it to an array but was unable to read or access the properties and the methods using the \ReflectionClass as it keeps returning

Class App\Controllers\AtController does not exist

This is the code I have so far

// 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


Solution

  • This issue, to my experience, usually comes from 2 sources:

    1. the incorrect setup of autoloading; and
    2. typo in the class name or namespace in the class file.

    If it is the 1st issue, adding a simple line in your file would fix the issue:

    <?php
    
    $cur_dir = __DIR__;
    
    // 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 = '';
    $routeScript = '';
    
    // Loop over the $classControllers
    foreach($classControllers as $classController){
        if($classController != "." && $classController != "..")
        {
            include_once $classController; // <-- this line
            $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;
        }
    }
    

    Please also check the second issue. Both namespace and class name are case sensitive. It is easy to have typo for both of them:

    <?php
    
    namespace App\Controllers;
    
    class AtController {
       // ...
    }