Autoloader is a strategy for finding PHP class or interface and loading it
at run time when a class is instantiated.
Better than writing multiple require and include as they do not scale well and is a clutter.
Make 2 files ⇒ Greet.php(This is the class file we want to load) and
mainFile.php(It will consume/use Greet.php)
Note: Keep file name and class name same.
Greet.php
<?php
class Greet{
public function sayHello(){
echo "Hello World From autoload";
}
}
?>
mainFile.php
<?php
spl_autoload_register(function($class){
require_once "{$class}.php";
});
$ob = new Greet();
$ob->sayHello();//Output : Hello World From autoload
?>