PHP laravel Source Read 3 (registered automatic loading)

Source: Internet
Author: User

According to the code for the portal file:

__dir__. ' /.. /vendor/autoload.php ';

Look at the autoload.php.

<?php

//autoload.php @generated by Composer

require_once __dir__. '/composer/autoload_real.php ';

return Composerautoloaderinit01f7a204137bd9325ebf450e4838e63c::getloader ();

This is the autoload.php source code.

Looks like we need to see autoload_real.php.

<?php//autoload_real.php @generated by Composer class Composerautoloaderinit01f7a204137bd9325ebf450e4838e63c {

    private static $loader;
            public static function Loadclassloader ($class) {if (' composer\autoload\classloader ' = = = $class) { Require __dir__.
        '/classloader.php '; ///First used method public static function Getloader () {if (Null!== self:: $loader) {Retu
        RN self:: $loader; //Register the automatic load function, the specific usage can see my other Bowen Spl_autoload_register (' composerautoloaderinit01f7a204137bd9325ebf450e483
        8e63c ', ' Loadclassloader '), true, true);
        The class that implements the automatic loading class is basically the automatic loading of this thing implementation self:: $loader = $loader = new \composer\autoload\classloader (); Deletes the spl_autoload_unregister (' composerautoloaderinit01f7a204137bd9325ebf450e4838e63c ', ' LoadClassL ') before the automatic load.

        Oader ')); Mainly based on the different environment to enable different automatic loading mode//php_version_id PHP version//hhvm_version is a Facebook open source project for PHP to raise performanceTo find out whether or not to use the Zend Guard encoding///If someone wants to know why to judge these four items. Don't ask me. Look down $useStaticLoader = php_version_id >= 50600 &&!defined (' hhvm_version ') && (!f Unction_exists (' zend_loader_file_encoded ') | |
        !zend_loader_file_encoded ()); Although it is two branches.
        But the end is to automatically load the class, deployment of some configuration variables, in order to automatically load the time to find the appropriate files, the files can be found, according to the rules configured into the variable. if ($useStaticLoader) {require_once __dir__.
            '/autoload_static.php '; Call_user_func (\composer\autoload\composerstaticinit01f7a204137bd9325ebf450e4838e63c::getinitializer ($loader))
        ; else {$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); }//Configure the variable, which is the class you want to use, and then just register the auto load function.

        When you instantiate a nonexistent class, start looking for the class, and the specific code is posted below $loader->register (true); Some class files must be used by the registration framework if ($useStaticLoader) {$includeFiles = composer\autoload\composerstaticinit01f7a20413
        7BD9325EBF450E4838E63C:: $files; else {$includeFiles = require __dir__.
        '/autoload_files.php '; foreach ($includeFiles as $fileIdentifier => $file) {composerrequire01f7a204137bd9325ebf450e4838
        E63C ($fileIdentifier, $file);
    return $loader; }} function composerrequire01f7a204137bd9325ebf450e4838e63c ($fileIdentifier, $file) {if empty ($GLOBALS [' __composer

        _autoload_files ' [$fileIdentifier])) {require $file;
    $GLOBALS [' __composer_autoload_files '] [$fileIdentifier] = true;
 }
}

Look at the classloader.php.

/** * Finds the path to the file where the class is defined.
     * * @param string $class The name of the class * * @return string|false The path if found, False otherwise */Public Function FindFile ($class) {//class map Lookup//////In the CLA, based on the previously configured variables
        Ssmap Find if (isset ($this->classmap[$class)) {return $this->classmap[$class]; ///Find if in classmapauthoritative ($this->classmapauthoritative | | isset ($this->missingclasses[$class]
        ) {return false; ///In Apcuprefix find if (null!== $this->apcuprefix) {$file = Apcu_fetch ($this->apcuprefix
            . $class, $hit);
            if ($hit) {return $file;

        }///Find the location of the specific class file according to the PSR0 and PSR4 rules $file = $this->findfilewithextension ($class, '. php '); Search for Hack files If we are running on HHVM if (false = = = $file;& defined (' hhvm_version ')) {$file = $this->findfilewithextension ($class, '. hh ');
        } if (null!== $this->apcuprefix) {apcu_add ($this->apcuprefix. $class, $file);
            } if (false = = $file) {//Remember that this class does not exist.
        $this->missingclasses[$class] = true;
    return $file; }

The other parts of the code do not explain, is not very complicated, follow the code can be read.

Summarize:

In these files, the main task is to do a job that automatically loads the class, allowing the program to temporarily load the desired files as needed.

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.