title: Aliasing (use ... as ...) all files in a directory
body: <p>I'm currently trying to optimise the framework we develop websites with, and one of the thing that bothers me is the aliasing of our classes. Right now we have one huge list of all the classes that are needed, and we have to add/remove classes based on necessity.</p>
  
  <p>I would like it to go as automated as possible. We have four folders with classes that are used on each website, so I tried the following:</p>
  
  <pre><code>$directories = array(
      '../classes/site/database', 
      '../classes/site/utils', 
      '../classes/creabea/utils', 
      '../classes/creabea/database'
  );
  
  foreach($directories as $dir){
      $dir_contents = new DirectoryIterator($dir);
      foreach($dir_contents as $item){
          if(!$item-&gt;isDot()){
              if($item-&gt;isDir()){
                  foreach(new DirectoryIterator($item-&gt;getPath().'/'.$item-&gt;__toString()) as $file_l2){ 
                      if(!$file_l2-&gt;isDot()){
                          $temppath = preg_replace('(\.\./)', '', $file_l2-&gt;getPath());
                          $path = preg_replace('/\//g', '\\', $temppath);
                          $classname = preg_replace('\.class\.php', '', $file_l2-&gt;__toString());
                          use $path.'\\'.$classname;
                      }
                  }
              } else {
                  $temppath = preg_replace('(\.\./)', '', $item-&gt;getPath());
                  $path = preg_replace('/\//g', '\\', $temppath);
                  $classname = preg_replace('\.class\.php', '', $item-&gt;__toString());
                  use $path.'\\'.$classname;
              }
          }
      }
  }
  </code></pre>
  
  <p>In the end it didn't work because you can't call <code>use</code> inside of functions or loops or things like that: <code>use</code> always has to be in the global scope.</p>
  
  <p>Is there another way to make the aliasing an automated process, while still maintaining the global scope?</p>