Autoloader.php 847 B

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