Question

I have this error when I want to autoload classes with RecursiveIteratorIterator and spl_autoload_register,

uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(): The system cannot find the path specified. (code: 3)

What does it mean?

Below is my class autoloader,

function autoload_multiple_directory($class_name){

    // List all the class directories in the array.
    $array_directories = array(
        'core/controller/', 
        'core/model/',
        'core/helper/',
        'core/ext/'
    );

    $parts = explode('\\', $class_name);

    // Set the class file name.
    $file_name = end($parts).'.php';

    foreach($array_directories as $path_directory){

        $iterator = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($path_directory),
            RecursiveIteratorIterator::SELF_FIRST
        );

        foreach ($iterator as $fileObject) {
            if ($fileObject->isDir()) {
                $files[] = str_replace('\\', '/', $fileObject->getPathname()).'/';
            }
        }


    }



    $array_directories = array_merge($array_directories,$files);

    // Loop the array.
    foreach($array_directories as $path_directory){
        if(file_exists(WEBSITE_DOCROOT.$path_directory.$file_name)){

            include WEBSITE_DOCROOT.$path_directory.$file_name;
        } 
    }
}


spl_autoload_register('autoload_multiple_directory');

The error line is pointing to new RecursiveDirectoryIterator($path_directory), why?

No correct solution

OTHER TIPS

I must use absolute path in new RecursiveDirectoryIterator(WEBSITE_DOCROOT.$path_directory) as the init.php is called via ajax sometimes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top