morrisonlevi

autoload.php

Oct 28th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class FileNotFoundException extends Exception{}
  2.  
  3. function __autoload($ClassName) {
  4.     $classPath = join(DIRECTORY_SEPARATOR, explode('\\', trim($ClassName,'\\')));
  5.     $extentions = explode(',', spl_autoload_extensions());
  6.    
  7.     $includePaths = explode(PATH_SEPARATOR, get_include_path());
  8.    
  9.     foreach($includePaths as $key=>$path) {
  10.         $includePaths[$key] = rtrim($path, DIRECTORY_SEPARATOR);
  11.     }
  12.    
  13.     foreach ($includePaths as $path) {
  14.         foreach ($extentions as $ext):
  15.             $file = $path.DIRECTORY_SEPARATOR.$classPath.$ext;
  16. //          echo "Trying $file . . .\n";
  17.             if (is_file($file)) {
  18. //              echo "Found $file . . .\n\n";
  19.                 require $file;
  20.                 return;
  21.             }
  22.         endforeach;
  23.     }
  24.    
  25.     throw new FileNotFoundException("Couldn't find $classPath using extentions: (". join('|', $extentions).').');
  26. }
  27. spl_autoload_extensions('.class.php,.php,.inc');
  28. spl_autoload_register('__autoload', true);
  29.  
Advertisement
Add Comment
Please, Sign In to add comment