35 private $fileExtension =
'.php';
38 private $namespaceSeparator =
'\\';
49 public function __construct($ns = null, $includePath = null)
51 $this->
namespace = $ns;
52 $this->includePath = $includePath;
65 public static function addLoader($namespace,
$path, $separator =
'\\', $extension =
'.php')
67 $loaderClass = get_called_class();
68 $loader =
new $loaderClass($namespace,
$path);
69 $loader->setNamespaceSeparator($separator);
70 $loader->setFileExtension($extension);
81 public function setNamespaceSeparator($sep)
83 $this->namespaceSeparator = $sep;
91 public function getNamespaceSeparator()
93 return $this->namespaceSeparator;
103 public function setIncludePath($includePath)
105 $this->includePath = $includePath;
113 public function getIncludePath()
115 return $this->includePath;
125 public function setFileExtension($fileExtension)
127 $this->fileExtension = $fileExtension;
135 public function getFileExtension()
137 return $this->fileExtension;
145 public function register()
147 spl_autoload_register(array($this,
'loadClass'));
155 public function unregister()
157 spl_autoload_unregister(array($this,
'loadClass'));
167 public function loadClass($className)
169 if (null === $this->
namespace
170 || $this->
namespace.$this->namespaceSeparator
171 === substr($className, 0, strlen($this->
namespace.$this->namespaceSeparator))
175 if (
false !== ($lastNsPos = strripos($className, $this->namespaceSeparator))) {
176 $namespace = substr($className, 0, $lastNsPos);
177 $className = substr($className, $lastNsPos + 1);
178 $fileName = str_replace(
179 $this->namespaceSeparator,
186 $absolute = ($this->includePath !== null ? $this->includePath .
DIRECTORY_SEPARATOR :
'') . $fileName;
187 if (file_exists($absolute)) {
defined('DS') or define('DS' DIRECTORY_SEPARATOR