Advertisement
Guest User

Loader Class

a guest
Apr 27th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | None | 0 0
  1. <?php
  2.  
  3. namespace System\Core;
  4.  
  5. class Loader
  6. {
  7.     private static $fileExtension = '.php';
  8.  
  9.  
  10.     private static function autoload($className)
  11.     {
  12.         $fileName = '';
  13.         $namespace = '';
  14.         if (false !== ($lastNsPos = strripos($className, '\\'))) {
  15.             $namespace = substr($className, 0, $lastNsPos);
  16.             $className = substr($className, $lastNsPos + 1);
  17.             $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
  18.         }
  19.         $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . self::$fileExtension;
  20.  
  21.         try {
  22.             include BASEPATH . $fileName;
  23.         }
  24.         catch (Exception $e) {
  25.             echo 'Loader::autoload exception catched: ', $e->getMessage(), '<br><br>';
  26.         }
  27.     }
  28.  
  29.     public static function splRegister()
  30.     {
  31.         spl_autoload_register('self::autoload');
  32.     }
  33.  
  34.     public static function splUnRegister()
  35.     {
  36.         spl_autoload_unregister('self::autoload');
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement