title: Cannot redeclare class: how to autoload a class if exists already in a folder?
body: <p>How can I check <strong>if</strong> a class exists already in a folder then <strong>do not</strong> load this class again from another folder?</p>
  
  <p>I have this folder structure for instance,</p>
  
  <pre><code>index.php
    code/
    local/
  </code></pre>
  
  <p>And I have these two <strong>identical</strong> classes in <code>code/</code> and <code>local/</code></p>
  
  <p>from local/</p>
  
  <pre><code>class Article
  {
      public function getArticle()
      {
          echo 'class from local';
      }
  }
  </code></pre>
  
  <p>from core,</p>
  
  <pre><code>class Article
  {
      public function getArticle()
      {
          echo 'class from core';
      }
  }
  </code></pre>
  
  <p>So I need a script that can detects the class of Article in <code>local/</code> - <strong>if</strong> it exits already in that folder than don't load the class again from <strong>core/</strong> folder. Is it possible? </p>
  
  <p>This is my autoload function in <code>index.php</code> for loading classes,</p>
  
  <pre><code>define ('WEBSITE_DOCROOT', str_replace('\\', '/', dirname(__FILE__)).'/');
  
  function autoloadMultipleDirectory($class_name) 
  {
      // List all the class directories in the array.
      $main_directories = array(
          'core/', 
          'local/'
      );
  
      // Set other vars and arrays.
      $sub_directories = array();
  
      // 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);
  
      // Set the class file name.
      $file_name = end($parts).'.php';
  
  
      // List any sub dirs in the main dirs above and store them in an array.
      foreach($main_directories as $path_directory)
      {
          $iterator = new RecursiveIteratorIterator
          (
              new RecursiveDirectoryIterator(WEBSITE_DOCROOT.$path_directory), // Must use absolute path to get the files when ajax is used.
              RecursiveIteratorIterator::SELF_FIRST
          );
  
          foreach ($iterator as $fileObject) 
          {
              if ($fileObject-&gt;isDir()) 
              {
                  // Replace any backslash to '/'.
                  $pathnameReplace = str_replace('\\', '/', $fileObject-&gt;getPathname());
                  //print_r($pathnameReplace);
  
                  // Explode the folder path.
                  $array = explode("/",$pathnameReplace);
  
                  // Get the actual folder.
                  $folder = end($array);
                  //print_r($folder);
  
                  // Stop proccessing if the folder is a dot or double dots.
                  if($folder === '.' || $folder === '..') {continue;} 
                  //var_dump($fileObject-&gt;getPathname());
  
                  // Must trim off the WEBSITE_DOCROOT. 
                  $sub_directories[] = preg_replace('~.*?(?=core|local)~i', '', str_replace('\\', '/', $fileObject-&gt;getPathname())) .'/';
              }
          }
      }
  
  
      // Mearge the main dirs with any sub dirs in them.
      $merged_directories = array_merge($main_directories,$sub_directories);
  
  
      // Loop the merge array and include the classes in them.
      foreach($merged_directories as $path_directory)
      {
          if(file_exists(WEBSITE_DOCROOT.$path_directory.$file_name))
          {
              // There is no need to use include/require_once. Autoload is a fallback when the system can't find the class you are instantiating. 
              // If you've already included it once via an autoload then the system knows about it and won't run your autoload method again anyway. 
              // So, just use the regular include/require - they are faster.
              include WEBSITE_DOCROOT.$path_directory.$file_name;
          } 
      }
  }
  
  // Register all the classes.
  spl_autoload_register('autoloadMultipleDirectory');
  $article = new Article();
  echo $article-&gt;getArticle();
  </code></pre>
  
  <p>of course I get this error, </p>
  
  <pre><code>Fatal error: Cannot redeclare class Article in C:\wamp\...\local\Article.php on line 3
  </code></pre>
  
  <p><a href="http://www.php.net/manual/en/function.class-exists.php" rel="nofollow">class_exists</a> seems to be the answer I should look into, but how can I use it with the function above, <strong>especially</strong> with <code>spl_autoload_register</code>. Or if you have any better ideas?</p>