Under Voltage
Under Voltage

Reputation: 43

How spl_autoload_register works?

Today I met with the spl_autoload_register. Well, Its functionality and flexibility are really great.

However, I just wonder how this SPL function works. Lets say I have a file that is named as Animal.php. On the another hand, I have another file that Index.php includes the code in the below.

spl_autoload_register(function($class) {
   require_once($class.'.php');
});

$animal = new Animal;
$animal->speak();

This SPL function understand that I create an object from Animal class and calls the Animal.php file which it should be called.

What happens the behind the scenes of this function? I mean how does it resolves that I need Animal.php file? I don't assign any value to $class variable or pass anything to anonymous function. But somehow, it understands what i need.

What is explanation of this?

Upvotes: 0

Views: 45

Answers (0)

Related Questions