Detailed description of Laravel's autoload implementation method, laravelautoload
After using Laravel for a while, I found that there were few include and require. I felt a little strange. I thought about how Laravel imported files.
In fact, Laravel still uses include or require, but all of them are written in a function.
Like this:
Function autoload ($ path) {require_once ($ path );}
Generally, there are more than one or two PHP files imported each time. Therefore, you need to define an array to store the "ing relationship between classes and PHP files", as shown in the following figure:
$ Classmap = ['db' => 'api. php', 'filesystem' => 'api. php', 'session '=> 'api. php'];
Then, traverse the array and do not write the for loop again. You can use an elegant function to solve the problem.
Array_map ("autoload", $ classmap );
Let's take a look at how the real Laravel defines this array, and the ing file is in \ vendor \ composer's autoload_classmap.php.