How to implement composer automatic loading in laravel framework

Source: Internet
Author: User
This article mainly introduces about how to achieve Laravel framework composer automatic loading, has a certain reference value, now share to everyone, there is a need for friends can refer to

Laravel as a popular PHP framework at home and abroad, elegant style, it has its own characteristics. The following article mainly introduces the Laravel framework of the composer automatic loading implementation of the relevant information, the text through the sample code introduced in very detailed, the need for friends can refer to.

Basis

Automatic loading allows you to load the required class files in a ready-to-load manner without having to write tedious require and include statements every time. Therefore, the execution of each request only loads the necessary classes, and does not care about the loading of the class, as long as it is necessary to use it directly.

The Laravel framework is an automatic load that is implemented through composer.

is implemented by the following code.

Require_once __dir__. '/composer '. '/autoload_real.php '; return Composerautoloaderinit7b20e4d61e2f88170fbbc44c70d38a1f::getloader ();

First we explain the two functions of Spl_autoload_register and Spl_autoload_unregister.

Spl_autoload_register automatically registers one or more auto-load functions, which are typically run automatically when the class is instantiated.

Spl_autoload_unregister on the contrary.

Paste the code of my experiment:

This is autoload.php.

<?php/** * Created by Phpstorm. * User:administrator * DATE:2017/12/7 * time:14:10 */namespace app;class Autoload {public Function __construct () {
  
    $this->autoload (); The Public Function AutoLoad () {  ///Spl_autoload_register (' autoload ', ' SS '), true), triggers a fatal error and must bring a namespace  Spl_ Autoload_register (Array (' App\autoload ', ' SS '), true); } public Function ss () {  echo 666;  Exit }}
  

This is index.php.

<?php/** * Created by Phpstorm.  * User:administrator * DATE:2017/12/7 * time:14:10 */require ' autoload.php '; $autoload =new \app\autoload (); $b =new B ();// The auto-load function echo 77;exit is automatically run at this time;

Find the function of Getloader and analyze it:

 public static function Getloader () {if (Null!== self:: $loader) {return self:: $loader; }//Register the auto-load function, in the load or instantiate class, run the Loadclassloader function spl_autoload_register (Array ('  composerautoloaderinit7b20e4d61e2f88170fbbc44c70d38a1f ', ' Loadclassloader '), true, true);  Self:: $loader = $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) {composerrequire7b20e4d61e2f88170fbbc44c70d38a1f ($  Fileidentifier, $file); } return $loader; }}

/***** the part that surrounds the ClassLoader, mainly in the

$PREFIXESPSR 0, $PREFIXDIRSPSR 4, $classMap, and other properties to assign values. That is, loading some of the configured files, and then loading them or looking for files later, is to look for them from the loaded configuration file. The search for the classes to be loaded is implemented primarily through the Register function. Then analyze the register function.

Public Function Register ($prepend = False) {Spl_autoload_register (Array ($this, ' LoadClass '), true, $prepend);}

Discover that the LoadClass function in this class is actually registered as an auto-load function. Then began to analyze the LoadClass function, and finally through the FindFile to find the class.

Public Function FindFile ($class) {/////Special note parameter $class is the class name generated from the namespace, refer to the namespace attribute.  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 looks for the IF (Isset ($this->classmap[$class]) from the loaded Classmap {  return $this->classmap[$class];} if ($this->classmapauthoritative) {  return false;} Look for the file from the configuration file that you just loaded. First in accordance with the PSR4 rules to find, and then according to PSR0///Two different rules are the main way to handle the underline. $file = $this->findfilewithextension ($class, '. php '); Search for Hack files If we is running on HHVM if ($file = = = NULL && defined (' hhvm_version ')) {  $file = $t His->findfilewithextension ($class, '. hh '); if ($file = = = null) {  //Remember that this class does not exist.  return $this->classmap[$class] = false; } return $file;}

At this point the Register function analysis is complete. We then 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 loading the autoload_file.php file.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.