Laravel框架中composer自动加载的实现分析

所属分类: 网络编程 / PHP编程 阅读数: 1406
收藏 0 赞 0 分享

基础

自动加载允许你通过即用即加载的方式来加载需要的类文件,而不用每次都写繁琐的require 和include语句。因此,每一次请求的执行过程都只加载必须的类,也不不要关心类的加载问题,只要需要的时候直接使用即可。

laravel 框架是通过composer 实现的自动加载。

是通过  下面的代码实现的。

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

首先我们对spl_autoload_register和spl_autoload_unregister 这两个函数进行解释一下。

spl_autoload_register 自动注册 一个或多个 自动加载函数,这些函数一般在 实例化类的时候,自动运行。

spl_autoload_unregister 恰恰相反。

贴上我实验的代码:

这是autoload.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/7
 * Time: 14:10
 */
namespace app;
class Autoload {

 public function __construct()
 {
  $this->autoload();
 }
 public function autoload(){
  // spl_autoload_register(array('Autoload','ss'),true); 会触发致命错误,必须带上命名空间
  spl_autoload_register(array('app\Autoload','ss'),true);
 }
 public function ss(){
  echo 666;
  exit;
 }
}

这是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();// 此时自动运行自动加载函数
echo 77;
exit;

找到getLoader 这个函数,并对其进行分析:

 public static function getLoader()
 {
  if (null !== self::$loader) {
   return self::$loader;
  }
  //注册自动加载函数,在加载或实例化类,运行loadClassLoader函数
  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; }}

/***** 包围的部分,主要对ClassLoader 中的

$prefixesPsr0   、$prefixDirsPsr4  、$classMap 等属性进行赋值。即加载一些配置好的文件,在后面进行加载或寻找文件时候,就是从加载的配置文件中寻找。寻找要加载的类主要通过register 函数来实现。然后分析register函数。

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

发现实际将该类中loadClass 函数注册为自动加载函数。于是开始分析loadClass函数,最终是通过findFile进行类的寻找。

public function findFile($class)
{
/// 特别注意 参数$class 是根据命名空间生成的class名称,具体请参考命名空间特性。
 // 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 首先从加载的classMap 中寻找
 if (isset($this->classMap[$class])) {
  return $this->classMap[$class];
 }
 if ($this->classMapAuthoritative) {
  return false;
 }
// 从刚才加载的配置文件中寻找文件。先按照 psr4 规则寻找,再按照psr0 寻找
// 两种规则的不同主要是对下划线的处理方式。
 $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;
}

至此register函数分析完。我们接着分析getLoader函数剩余代码。

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

这段代码其实就是加载autoload_file.php 文件。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

php实现二叉树中和为某一值的路径方法

在本篇文章中我们给大家分享了php实现二叉树中和为某一值的路径方法,有需要的朋友们可以参考下。
收藏 0 赞 0 分享

深入理解 PHP7 中全新的 zval 容器和引用计数机制

这篇文章主要介绍了 PHP7 中全新的 zval 容器和引用计数机制的相关知识, 主要侧重于解释新 zval 容器中的引用计数机制。需要的朋友可以参考下
收藏 0 赞 0 分享

PHP中使用CURL发送get/post请求上传图片批处理功能

这篇文章主要介绍了PHP中使用CURL发送get/post请求上传图片批处理 功能,本文通过实例代码给大家介绍的非常详细,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP实现用session来实现记录用户登陆信息

在本篇文章里我们给大家分享了关于PHP如何用session来实现记录用户登陆信息的知识点,有兴趣的朋友们参考下。
收藏 0 赞 0 分享

实现PHP中session存储及删除变量

在本篇文章中我们给大家分享了PHP中session如何存储及删除变量的相关知识点内容,有兴趣的朋友们参考下。
收藏 0 赞 0 分享

PHP使Laravel为JSON REST API返回自定义错误的问题

这篇文章主要介绍了PHP使Laravel为JSON REST API返回自定义错误的问题,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php 读取文件夹下所有图片、文件的实例

今天小编就为大家分享一篇php 读取文件夹下所有图片、文件的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

PHP使用glob方法遍历文件夹下所有文件的实例

今天小编就为大家分享一篇PHP使用glob方法遍历文件夹下所有文件的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Laravel关联模型中过滤结果为空的结果集(has和with区别)

这篇文章主要介绍了Laravel关联模型中过滤结果为空的结果集(has和with区别),需要的朋友可以参考下
收藏 0 赞 0 分享

django中的ajax组件教程详解

Ajax(Asynchronous Javascript And XML)翻译成英文就是“异步Javascript和XML”。这篇文章主要介绍了django中的ajax组件的教程 ,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多