MayoMayn
BestDev
- Oct 18, 2016
- 1,423
- 683
Basically a better version of spl_autoload_register since it only tries to require the class file whenever you initializes the class.
Here it automatically gets every file inside your class folder, requires the files and initializes it. All you have to do, is name the class the same as the file.
index.php
autoload.php
You can easily manage with a bit of coding, to require classes in different folders too.
Hopefully this can be of some use to any of you.
Here it automatically gets every file inside your class folder, requires the files and initializes it. All you have to do, is name the class the same as the file.
index.php
PHP:
// Require loader
require_once __DIR__ . '/app/autoload.php';
// Initialize loader
$loader = new Loader(false);
// Test
User::dump(['hello' => 'test']);
// Get count of how many classes there has been loaded
echo $loader->count;
PHP:
class Loader
{
private static $library = [];
protected static $classPath = __DIR__ . "/classes/";
protected static $interfacePath = __DIR__ . "/classes/interfaces/";
public $count = 1;
public function __construct($requireInterface = true)
{
if(empty(static::$library)) {
// Get all files inside the class folder
foreach(array_map('basename', glob(static::$classPath . "*.php", GLOB_BRACE)) as $classExt) {
// Get rid of php extension easily without pathinfo
$classNoExt = substr($classExt, 0, -4);
// Make sure the class is not already declared somewhere else
if(!in_array($classNoExt, get_declared_classes())) {
$file = static::$classPath . $classExt;
if($requireInterface) {
// Get interface file
$interface = static::$interfacePath . $classExt;
// Check if interface file exists
if(!file_exists($interface)) {
// Throw exception
die("Unable to load interface file: " . $interface);
}
// Require interface
require_once $interface;
//Check if interface is set
if(!interface_exists("Interface" . $classNoExt)) {
// Throw exception
die("Unable to find interface: " . $interface);
}
}
// Require class
require_once $file;
// Check if class file exists
if(class_exists($classNoExt)) {
// Set class // class.container.php
static::$library[$classNoExt] = new $classNoExt();
$this->count++;
} else {
// Throw error
die("Unable to load class: " . $classNoExt);
}
}
}
}
}
Hopefully this can be of some use to any of you.
Last edited: