Domanda

I want to loop all the sub dirs in the main dirs, where I keeps all my classes, for instance,

core/
   model/
       page/
          class_1.php
          class_2.php
       menu/
          class_3.php
       and so on...

So this is my autoload function that I place it in the init.php,

function autoload_multiple_directory($class_name){

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

    // When you use namespace in a class, you get something like this when you auto load that class \foo\tidy.
    // So use explode to split the string and then get the last item in the exloded array.
    $parts = explode('\\', $class_name);
    //print_r($parts);

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

    // Loop the array.
    foreach($array_directories as $path_directory){


        $recursive_directory = new RecursiveDirectoryIterator($path_directory);
        foreach (new RecursiveIteratorIterator($recursive_directory) as $filename => $file) {

            if(file_exists(WEBSITE_DOCROOT.$file->getPath().'/'.$file_name)){

                include WEBSITE_DOCROOT.$file->getPath().'/'.$file_name;
            } 

        }


        /* no problem with this, but I cannot loop the sub dirs...
        if(file_exists(WEBSITE_DOCROOT.$path_directory.$file_name)){

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


spl_autoload_register('autoload_multiple_directory');

But then I get this error message,

Fatal error: Cannot redeclare class Common in C:\wamp\www\xxx\core\helper\Common.php on line 6

There is only one Common class in my project. Why does it say more than once or redeclare?

But if you look at the if(file_exists(WEBSITE_DOCROOT.$path_directory.$file_name)) that I comment out - it has no problem to load the classes. The problem of this initial loop is that it does not loop the sub dirs in a main dir, for instance, core/model/

Any ideas why and what should I do to loop the sub dirs of a main dir?

EDIT:

The problem comes from RecursiveDirectoryIterator - it loops the directories and lists all files. But what I want is only the sub directories.

È stato utile?

Soluzione

Is there any chance that there are more than one copy of Common.php file exists in those folders?
Because your code does not break after including a class file the autoloader will continue seeking other files with the same name in the folder tree, and would lead to Fatal error: Cannot redeclare class XXX error. Adding break could fix the problem.

// Loop the array.
$isClassFound = false;
foreach($array_directories as $path_directory){
    $recursive_directory = new RecursiveDirectoryIterator($path_directory);
    foreach (new RecursiveIteratorIterator($recursive_directory) as $filename => $file) {
        if(file_exists(WEBSITE_DOCROOT.$file->getPath().'/'.$file_name)){
            include WEBSITE_DOCROOT.$file->getPath().'/'.$file_name;
            $isClassFound = true;
        }
        if ($isClassFound) break;
    }
    if ($isClassFound) break;
}

However, the nature of your autoloader seems like it should not allow any duplicated class file names. Maybe you can write a name duplication checker to guarantee uniqueness.

EDIT:

I removed the class_exists() part from my answer because using it doesn't make much sense. Anyway, since you saw that version of my answer, and you asked me where to put class_exists() via the comment, I'll revive the code sample. you can add the following code at the beginning of the autoloader.

if (class_exists($class_name,false)) // give false to avoid automatic loading
    return;

Altri suggerimenti

Open the start menu. In the text box write cmd, wait for the cmd program to pop up and then hit Enter.

Once the terminal window opens navigate to your root folder and then do a recursive search (through all files and folders) for the class' name:

cd C:\wamp\www\xxx
findstr /SNIP /C:"class common" *.php

There should be more than one declaration of the class.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top