Skip to content

Instantly share code, notes, and snippets.

@RStankov
Created June 4, 2011 09:08
Show Gist options
  • Select an option

  • Save RStankov/1007740 to your computer and use it in GitHub Desktop.

Select an option

Save RStankov/1007740 to your computer and use it in GitHub Desktop.
<?php
class Loader {
private static $loadPaths = array();
static function autoload($class){
// assume its name is its path (ActiveRecord_Relation_BelongsTo is in ActiveRecord/Relation/BelongsTo.php)
$fileName = str_replace('_', '/', $class). '.php';
foreach (self::$loadPaths as $path){
if (file_exists($src = $path . '/' . $fileName)){
require $src;
return;
}
}
}
static function addPath($path){
self::$loadPaths[] = $path;
}
static function startAutoLoadingFrom(array $loadPaths){
self::$loadPaths = $loadPaths;
spl_autoload_register(array('Loader', 'autoload'));
}
}
<?php
Loader::startAutoLoadingFrom(array(
ROOT_DIR . '/app/models',
ROOT_DIR . '/app/controllers',
ROOT_DIR . '/lib',
ROOT_DIR . '/vendor'
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment