Files
predis/lib/Predis/Autoloader.php
T
Eric Naeseth 94a2ddeca9 Add an autoloader for Predis classes.
This makes it easier to use Predis in scripts and projects that do
not already include as PSR-0 autoloader.

Signed-off-by: Eric Naeseth <eric@thumbtack.com>
2011-09-27 17:42:06 -07:00

34 lines
847 B
PHP

<?php
namespace Predis;
class Autoloader {
private $base_directory;
private $prefix;
public function __construct($base_directory=NULL) {
$this->base_directory = $base_directory ?: dirname(__FILE__);
$this->prefix = __NAMESPACE__ . '\\';
}
public static function register() {
spl_autoload_register(array(new self, 'autoload'));
}
public function autoload($class_name) {
if (0 !== strpos($class_name, $this->prefix)) {
return;
}
$relative_class_name = substr($class_name, strlen($this->prefix));
$class_name_parts = explode('\\', $relative_class_name);
$path = $this->base_directory .
DIRECTORY_SEPARATOR .
implode(DIRECTORY_SEPARATOR, $class_name_parts) .
'.php';
require_once $path;
}
}