Skip to content

Instantly share code, notes, and snippets.

@Firestorm-Graphics
Created July 7, 2017 11:04
Show Gist options
  • Save Firestorm-Graphics/479f75d6b52ee622bac51c80321f8448 to your computer and use it in GitHub Desktop.
Save Firestorm-Graphics/479f75d6b52ee622bac51c80321f8448 to your computer and use it in GitHub Desktop.
Here’s an autoloader class I came up with for one of my framework-free PHP projects. Autoloading is a way to let PHP know how you’ve architected your class file locations hierarchy by supplying it with a function to run. This function will handle the including of the class file. This is awesome because we don’t need to hard code every file inclu…
<?php
class Autoloader {
static public function loader($className) {
$filename = "Classes/" . str_replace("\\", '/', $className) . ".php";
if (file_exists($filename)) {
include($filename);
if (class_exists($className)) {
return TRUE;
}
}
return FALSE;
}
}
spl_autoload_register('Autoloader::loader');
?>
require_once("Classes/Autoloader.php");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment