Autoloader.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. /**
  8. * Description of Autoloader
  9. * 自动加载器
  10. * @author jgao
  11. */
  12. class Autoloader
  13. {
  14. private $directorys;
  15. public function __construct() {
  16. $this->directorys = array(
  17. __DIR__."/utils/",
  18. __DIR__."/models/",
  19. __DIR__."/providers/",
  20. );
  21. }
  22. /**
  23. * Registers the autoloader class with the PHP SPL autoloader.
  24. *
  25. * @param bool $prepend Prepend the autoloader on the stack instead of appending it.
  26. */
  27. public static function register($prepend = false){
  28. spl_autoload_register(array(new self(), 'autoload'), true, $prepend);
  29. }
  30. /**
  31. * Loads a class from a file using its fully qualified name.
  32. *
  33. * @param string $className Fully qualified name of a class.
  34. */
  35. public function autoload($className) {
  36. foreach ($this->directorys as $dir){
  37. $filepath = $dir.$className.'.php';
  38. if (is_file($filepath)) {
  39. require $filepath;
  40. break;
  41. }
  42. }
  43. }
  44. }