-
-
Save kriswallsmith/231120 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SplClassLoader | |
{ | |
protected | |
$includePath = null, | |
$namespace = null, | |
$extension = '.php', | |
$namespaceSeparator = '\\'; | |
public function __construct($includePath = null, $namespace = null) | |
{ | |
$this->includePath = $includePath; | |
$this->namespace = $namespace; | |
$this->register(); | |
} | |
public function setNamespaceSeparator($namespaceSeparator) | |
{ | |
$this->namespaceSeparator = $namespaceSeparator; | |
} | |
public function getNamespaceSeparator() | |
{ | |
return $this->namespaceSeparator; | |
} | |
public function setIncludePath($includePath) | |
{ | |
$this->includePath = $includePath; | |
} | |
public function getIncludePath() | |
{ | |
return $this->includePath; | |
} | |
public function setFileExtension($extension) | |
{ | |
$this->extension = $extension; | |
} | |
public function getFileExtension($extension) | |
{ | |
return $this->extension; | |
} | |
public function register() | |
{ | |
spl_autoload_register(array($this, 'loadClass')); | |
} | |
public function unregister() | |
{ | |
spl_autoload_unregister(array($this, 'loadClass')); | |
} | |
public function loadClass($class) | |
{ | |
if (null === $this->namespace || 0 === strpos($class, $this->namespace.$this->namespaceSeparator)) | |
{ | |
$includePath = $this->includePath ? $this->includePath.'/' : ''; | |
require $includePath.str_replace($this->namespaceSeparator, '/', $class).$this->extension; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment