Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. class Autoloader
  2. {
  3.  
  4. public function __construct()
  5. {
  6. spl_autoload_register( array( $this, 'autoload' ) );
  7. }
  8.  
  9. public function autoload( $class )
  10. {
  11. $iterator = new RecursiveDirectoryIterator( LIBRARY_PATH );
  12.  
  13. foreach( new RecursiveIteratorIterator( $iterator ) as $file=>$meta ) {
  14.  
  15. if( ( $class . '.php' ) === $meta->getFileName() ) {
  16.  
  17. if( file_exists( $file ) ) {
  18. require_once $file;
  19. }
  20. break;
  21. }
  22. }
  23.  
  24. unset( $iterator, $file );
  25. }
  26.  
  27. }
  28.  
  29. class SimpleClassLoader
  30. {
  31. /**
  32. * Installs this class loader on the SPL autoload stack.
  33. */
  34. public function register()
  35. {
  36. spl_autoload_register(array($this, 'loadClass'));
  37. }
  38.  
  39. /**
  40. * Uninstalls this class loader from the SPL autoloader stack.
  41. */
  42. public function unregister()
  43. {
  44. spl_autoload_unregister(array($this, 'loadClass'));
  45. }
  46.  
  47. /**
  48. * Loads the given class or interface.
  49. *
  50. * @param string $className The name of the class to load.
  51. * @return void
  52. */
  53. public function loadClass($className)
  54. {
  55. require str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
  56. }
  57. }
  58.  
  59. $loader = new SimpleClassLoader();
  60. $loader->register();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement