Analysis on the principle of composer AutoLoad

Source: Internet
Author: User
Tags autoload
Recently read a little laravel code, which used to composer, the feeling of the autoload mechanism is a bit weird, browsing the source code, there is income, so recorded.
After you use composer to download the package, you need to require "vendor/autoload.php", so start with the entry file.
1.autoload.php => composer/autoload_real.php/getloader () => $loader = new \composer\autoload\classloader ();
2. Then according to the version of PHP will have different processing, but the real effect is the same, are to initialize $LOADER->PREFIXLENGTHSPSR4, $loader->prefixdirspsr4, $loader-> PrefixesPsr0 and
$loader->classmap These variables, just use different versions of PHP features to improve performance. The documents involved are autoload_namespaces.php,autoload_psr4.php,autoload_classmap.php.
These files primarily hold the correspondence between the start name of the class name and the file, such as the ' app\\ ' => array ($baseDir. '/app ', in essence, is all the occurrences of the class name starting with App\ under the folder app.
The location of the App\events\event class is naturally defined in the event file for the App\events folder.
3. Call $loader->register (TRUE), register the Composer\autoload\classloader\loadclass function as the AutoLoad function, and trigger the execution of the AutoLoad function when the class name is not found.
The 4.composer\autoload\classloader\loadclass function is mostly findfile&&includefile, and the way to find a file is to find the mapping relationship that is saved in the 2 sequence.
So that all namespace named rules are clear, such as the folder App\http folder under the new file kernel.php, you should define the class in kernel file kernel as follows
namespace App\http;
Class Kernel {

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: 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.