Detailed description of Laravel's autoload implementation method, laravelautoload

Source: Internet
Author: User
Tags autoload

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.