Skip to content

Instantly share code, notes, and snippets.

@achepukov
Last active September 2, 2016 12:40
Show Gist options
  • Save achepukov/881dd4e7884ae0230bd1c7ec8e933be9 to your computer and use it in GitHub Desktop.
Save achepukov/881dd4e7884ae0230bd1c7ec8e933be9 to your computer and use it in GitHub Desktop.
Simple php autoloader
<?php
/**
* Autoload:
* new A() -> A.php
* new B1\A1\A -> B1/A1/A.php
*/
class Autoload
{
protected $_basePath;
public function __construct($basePath = "./")
{
$this->_basePath = $basePath;
}
public function load($className) {
$path = $this->getPath($className);
if (file_exists($path)) {
require $path;
}
}
protected function getPath($className) {
return $this->_basePath . DIRECTORY_SEPARATOR .
str_replace('\\', DIRECTORY_SEPARATOR, $className) . ".php";
}
}
spl_autoload_register(array(new Autoload(getcwd()), 'load'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment