Last active
September 2, 2016 12:40
-
-
Save achepukov/881dd4e7884ae0230bd1c7ec8e933be9 to your computer and use it in GitHub Desktop.
Simple php autoloader
This file contains hidden or 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 | |
/** | |
* 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