Autoloader.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. private $directorys;
  14. public function __construct() {
  15. $this->directorys = array(
  16. __DIR__ . "/utils/",
  17. __DIR__ . "/models/",
  18. __DIR__ . "/providers/",
  19. );
  20. }
  21. /**
  22. * Registers the autoloader class with the PHP SPL autoloader.
  23. *
  24. * @param bool $prepend Prepend the autoloader on the stack instead of appending it.
  25. */
  26. public static function register($prepend = false) {
  27. spl_autoload_register(array(new self(), 'autoload'), true, $prepend);
  28. }
  29. /**
  30. * Loads a class from a file using its fully qualified name.
  31. *
  32. * @param string $className Fully qualified name of a class.
  33. */
  34. public function autoload($className) {
  35. foreach ($this->directorys as $dir) {
  36. $filepath = $dir . $className . '.php';
  37. if (is_file($filepath)) {
  38. require $filepath;
  39. break;
  40. }
  41. }
  42. }
  43. }