Skip to content

Instantly share code, notes, and snippets.

@stevenosse
Last active November 2, 2018 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenosse/f39f0dac070282189b1fefa27e229f5d to your computer and use it in GitHub Desktop.
Save stevenosse/f39f0dac070282189b1fefa27e229f5d to your computer and use it in GitHub Desktop.
Autoloader class that automatically loads all the classes of your project.
<?php
/*
* Author : Steve Nosse
* Licence : Freeware
* How to use ?
* Simply do require_once "autoloader.php"; and then Autoloader::init();
*/
class Autoloader {
/**
* File extension for the class files
*
* @var string
**/
protected static $extension = '.php';
/**
* Directory separator for the file names
*
* @var string
**/
protected static $dir_separator = '/';
/**
* Name separator for the namespaces in the file names
*
* @var string
**/
protected static $file_separator = '_';
/**
* @return boolean
**/
public static function init() {
return spl_autoload_register(__CLASS__ . '::load');
}
/**
* Load the class
*
* @param string $class The class to load
* @return void
**/
public static function load($class) {
$file = self::getFileName($class);
//if (file_exists($file)) {
require_once($file);
/*} else {
throw new Exception('Class ' . $class . ' not exists');
}*/
}
/**
* Generate the name of the class file
*
* @param string $class The class name
* @return string The file name
**/
protected static function getFileName($class) {
$file = str_replace(
self::$file_separator,
self::$dir_separator,
$class
);
return $file . self::$extension;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment