Implementation Analysis of automatic composer loading in Laravel framework, laravelcomposer

Source: Internet
Author: User
Tags autoload hhvm

Implementation Analysis of automatic composer loading in Laravel framework, laravelcomposer

Basic

Automatic Loading allows you to load the required class files by means of out-of-the-box loading, instead of having to write tedious require and include statements every time. Therefore, each request execution process only loads the required classes, and does not care about the loading of classes. You only need to use them when necessary.

The laravel framework is automatically loaded using composer.

It is implemented through the following code.

require_once __DIR__ . '/composer' . '/autoload_real.php';return ComposerAutoloaderInit7b20e4d61e2f88170fbbc44c70d38a1f::getLoader();

First, Let's explain the spl_autoload_register and spl_autoload_unregister functions.

Spl_autoload_register automatically registers one or more auto-load functions, which generally run automatically during class instantiation.

Spl_autoload_unregister is the opposite.

Paste the code of my experiment:

This is autoload. php

<? Php/*** Created by PhpStorm. * User: Administrator * Date: * Time: */namespace app; class Autoload {public function _ construct () {$ this-> autoload ();} public function autoload () {// spl_autoload_register (array ('autoload', 'ss'), true); a fatal error is triggered, the namespace spl_autoload_register (array ('app \ autoload', 'ss'), true);} public function ss () {echo 666; exit;} must be included ;}}

This is index. php.

<? Php/*** Created by PhpStorm. * User: Administrator * Date: * Time: */require 'autoload. php '; $ autoload = new \ app \ Autoload (); $ B = new B (); // The function echo 77; exit is automatically loaded;

Find the getLoader function and analyze it:

Public static function getLoader () {if (null! = Self ::$ loader) {return self: $ loader;} // register an automatic loading function. When loading or instantiating a class, run the loadClassLoader function spl_autoload_register (array ('upload', 'loadclassloader '), true, true); self: $ loader = new \ Composer \ Autoload \ ClassLoader (); spl_autoload_unregister (array ('composerautoloaderinit7b20e4d61e2f88170fbbc44c70d38a1f', 'loadclassloader ')); /******************** 1 ****************** * ************************************ $ map = require _ DIR __. '/autoload_namespaces.php'; foreach ($ map as $ namespace =>$ path) {$ loader-> set ($ namespace, $ path );} $ map = require _ DIR __. '/autoload_psr4.php'; foreach ($ map as $ namespace => $ path) {$ loader-> setPsr4 ($ namespace, $ path );} $ classMap = require _ DIR __. '/autoload_classmap.php'; if ($ classMap) {$ loader-> addClassMap ($ classMap );} /******************** 1 ****************** * ************************************ $ loader -> register (true ); $ includeFiles = require _ DIR __. '/autoload_files.php'; foreach ($ includeFiles as $ fileIdentifier => $ file) {delimiter ($ fileIdentifier, $ file);} return $ loader ;}}

/*****, Mainly for

$ PrefixesPsr0, $ prefixDirsPsr4, $ classMap, and other attributes are assigned values. That is, some configured files are loaded. When loading or searching for files later, they are searched from the loaded configuration files. The class to be loaded is mainly implemented through the register function. Then analyze the register function.

public function register($prepend = false){ spl_autoload_register(array($this, 'loadClass'), true, $prepend);}

It is found that the loadClass function in this class is actually registered as an automatic loading function. So I began to analyze the loadClass function and finally searched for classes through findFile.

Public function findFile ($ class) {// note that the $ class parameter is the class name generated based on the namespace. For details, refer to the namespace features. // Work around for PHP 5.3.0-5.3.2 https://bugs.php.net/50731 if ('\' = $ class [0]) {$ class = substr ($ class, 1 );} // class map lookup first searches for if (isset ($ this-> classMap [$ class]) from the loaded classMap) {return $ this-> classMap [$ class];} if ($ this-> classMapAuthoritative) {return false;} // find the file from the configuration file just loaded. Search by psr4 rules first, and then search by psr0 // The two rules are different mainly for the underline processing method. $ File = $ this-> findFileWithExtension ($ class ,'. php '); // Search for Hack files if we are running on HHVM if ($ file === null & defined ('hhvm _ version ')) {$ file = $ this-> findFileWithExtension ($ class ,'. hh ');} if ($ file = null) {// Remember that this class does not exist. return $ this-> classMap [$ class] = false;} return $ file ;}

Now the register function has been analyzed. We will analyze the remaining code of the getLoader function.

$includeFiles = require __DIR__ . '/autoload_files.php';foreach ($includeFiles as $fileIdentifier => $file) { composerRequire7b20e4d61e2f88170fbbc44c70d38a1f($fileIdentifier, $file);}

This code is actually used to load the autoload_file.php file.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

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.