Skip to content

Instantly share code, notes, and snippets.

@kriswallsmith
Forked from jwage/SplClassLoader.php
Created November 10, 2009 18:13
Show Gist options
  • Save kriswallsmith/231120 to your computer and use it in GitHub Desktop.
Save kriswallsmith/231120 to your computer and use it in GitHub Desktop.
<?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