PHP - Using the spl_autoload_register function

Introduction

You define your autoloader function with a valid name, and then invoke the function spl_autoload_register, sending the name of your autoloader as an argument.

You can call this function as many times as the different autoloaders you have in your code.


function autoloader($classname) { 
   $lastSlash = strpos($classname, '\\') + 1; 
   $classname = substr($classname, $lastSlash); 
   $directory = str_replace('\\', '/', $classname); 
   $filename = __DIR__ . '/' . $directory . '.php'; 
   require_once($filename); 
} 
spl_autoload_register('autoloader'); 

Related Topic