Home>

This article explains to you how to implement autoload in laravel?I found out after analysis,It's really clever.Let me give you a detailed explanation.

After using laravel for a while, I found that there are very few include and require.Think about how laravel does the file import.

In fact, Laravel still uses include or require, but it is written in a function.

like this:

function autoload ($path) {
require_once ($path);
}

Generally, more than one or two php files are imported each time.There will be many,So you need to define an array to store "the mapping relationship between classes and php files", like this:

$classmap=[
"db" =>"api.php","filesystem" =>"api.php","session" =>"api.php"
];

Then, iterate through the array,Don't write a for loop anymore, you can solve it with an elegant function.

array_map ("autoload", $classmap);

Let's see how real laravel defines this array,The mapping file is in autoload_classmap.php in \ vendor \ composer

php