It is a mechanism that allows to load multiple other PHP scripts automatically. It is difficult to use explicit declaration of include
or require
statements every time. It reduces the usages of explicit include
or require
statements. It is important for large scale applications to load multiple classes, interfaces and so on.
PHP provides a built-in autoloader that can be used to load classes based on their namespace and file paths automatically.
spl_autoload_register(function($classname) {
$classname .= ".php";
if (file_exists($classname)) {
require $classname;
}
});
// Now the class can be available without including the file
$obj = new MyNamespace\MyClass();
To autoload different namespace PHP scripts:
spl_autoload_register(function($classname) {
$classname = str_replace("\\", "/", $classname);
$classname .= ".php";
if (file_exists($classname)) {
require $classname;
}
});
// Now the class can be available without including the file
$obj = new MyClass();
To autoload different namespace on specific directory:
spl_autoload_register(function ($class) {
$file = __DIR__ . '/src/' . str_replace('\\', '/', $class) . '.php';
if (file_exists($file)) {
require_once $file;
}
});
- When try to use a class, PHP checks for the class is already defined.
- If the class is missing, then registered autoloader is triggered.
- The autoloader searches for a file with specified name as the class in the specified directory.
- If the file is found, it adds, making the class available.
- If the class is not found throws an error.