Autoloader.php 861 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace Predis;
  3. class Autoloader
  4. {
  5. private $base_directory;
  6. private $prefix;
  7. public function __construct($base_directory = null)
  8. {
  9. $this->base_directory = $base_directory ?: dirname(__FILE__);
  10. $this->prefix = __NAMESPACE__ . '\\';
  11. }
  12. public static function register()
  13. {
  14. spl_autoload_register(array(new self, 'autoload'));
  15. }
  16. public function autoload($class_name)
  17. {
  18. if (0 !== strpos($class_name, $this->prefix)) {
  19. return;
  20. }
  21. $relative_class_name = substr($class_name, strlen($this->prefix));
  22. $class_name_parts = explode('\\', $relative_class_name);
  23. $path = $this->base_directory .
  24. DIRECTORY_SEPARATOR .
  25. implode(DIRECTORY_SEPARATOR, $class_name_parts) .
  26. '.php';
  27. require_once $path;
  28. }
  29. }