Autoloader.php 834 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace Predis;
  3. class Autoloader
  4. {
  5. private $_baseDir;
  6. private $_prefix;
  7. public function __construct($baseDirectory = null)
  8. {
  9. $this->_baseDir = $baseDirectory ?: 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($className)
  17. {
  18. if (0 !== strpos($className, $this->_prefix)) {
  19. return;
  20. }
  21. $relativeClassName = substr($className, strlen($this->_prefix));
  22. $classNameParts = explode('\\', $relativeClassName);
  23. $path = $this->_baseDir .
  24. DIRECTORY_SEPARATOR .
  25. implode(DIRECTORY_SEPARATOR, $classNameParts) .
  26. '.php';
  27. require_once $path;
  28. }
  29. }