Composer 的autoload 实现

 
 
require_once './vendor/autoload.php';

一、autoload.php
   加载 composer/autoload_real.php 调用 autoload_real.php 下的 getLoader() 方法
  
二、autoload_real.php
    getLoader() 方法
        创建 composer\ClassLoader.php 的对象 $loader
1、静态调用 $useStaticLoader 加载 composer\autoload_static.php 回调 autoload_static.php 下的 getInitializer($loader) 方法 getInitializer() 方法 通过匿名函数绑定,为$loader 对象下的私有属性赋值 $prefixLengthsPsr4, $prefixDirsPsr4, $prefixDirsPsr4, $classMap 注册自动加载 $loader->register(true);
spl_autoload_register([
$this, 'loadClass', true, $prepend]);
function loadClass($class) { if ($file = $this->findFile($class)) { includeFile($file) return true; } }
     // ClassLoader.php 下的方法
public function findFile($class) { // class map lookup if (isset($this->classMap[$class])) { return $this->classMap[$class]; } if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { return false; } if (null !== $this->apcuPrefix) { $file = apcu_fetch($this->apcuPrefix.$class, $hit); if ($hit) { return $file; } } $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; } function includeFile($file) { include $file; } 2非静态调用 略

猜你喜欢

转载自www.cnblogs.com/cshaptx4869/p/10538256.html