Loader.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Qii\Autoloader;
  3. /**
  4. * Loader类
  5. */
  6. class Loader
  7. {
  8. const VERSION = '1.2';
  9. /**
  10. * @var $loaded 保存已经加载过的类实例
  11. */
  12. private $loaded;
  13. /**
  14. * @var $lastCall 最后一次访问的方法
  15. */
  16. private $lastCall;
  17. public function __construct()
  18. {
  19. $this->lastCall = null;
  20. return $this;
  21. }
  22. public function __clone()
  23. {
  24. $this->lastCall = null;
  25. }
  26. public function __get($name)
  27. {
  28. $this->lastCall = $name;
  29. return $this;
  30. }
  31. /**
  32. * 返回load对象
  33. *
  34. * @return mixed
  35. */
  36. public static function Instance()
  37. {
  38. return \Qii\Autoloader\Instance::instance('\Qii\Autoloader\Loader');
  39. }
  40. /**
  41. * 实现自动加载
  42. *
  43. * @param $method
  44. * @param $argvs
  45. * @return object
  46. */
  47. public function __call($method, $args)
  48. {
  49. $class = array_shift($args);
  50. if ($this->lastCall) {
  51. $className = $this->lastCall . '\\' . $method;
  52. \Qii\Autoloader\Import::requireByClass($className);
  53. } else {
  54. $className = $method . '\\' . $class;
  55. }
  56. $this->lastCall = null;
  57. if (isset($this->loaded[$className])) return $this->loaded[$className];
  58. return $this->loaded[$className] = call_user_func_array(array('\Qii\Autoloader\Instance', 'Instance'), array_merge(array($className), $args));
  59. }
  60. }