<?php namespace Ctct; class SplClassLoader { private $fileExtension = '.php'; private $namespace; private $includePath; private $namespaceSeparator = '\\'; public function __construct($ns = null, $includePath = null) { $this->namespace = $ns; $this->includePath = $includePath; } public function setNamespaceSeparator($sep) { $this->namespaceSeparator = $sep; } public function getNamespaceSeparator() { return $this->namespaceSeparator; } public function setIncludePath($includePath) { $this->includePath = $includePath; } public function getIncludePath() { return $this->includePath; } public function setFileExtension($fileExtension) { $this->fileExtension = $fileExtension; } public function getFileExtension() { return $this->fileExtension; } public function register() { spl_autoload_register(array($this, 'loadClass')); } public function unregister() { spl_autoload_unregister(array($this, 'loadClass')); } public function loadClass($className) { if (null === $this->namespace || $this->namespace . $this->namespaceSeparator === substr($className, 0, strlen($this->namespace . $this->namespaceSeparator)) ) { $fileName = ''; $namespace = ''; if (false !== ($lastNsPos = strripos($className, $this->namespaceSeparator))) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $fileName = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->fileExtension; require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '') . $fileName; } } } 