根據入口檔案的代碼:
require __DIR__.'/../vendor/autoload.php';
看下autoload.php
<?php// autoload.php @generated by Composerrequire_once __DIR__ . '/composer/autoload_real.php';return ComposerAutoloaderInit01f7a204137bd9325ebf450e4838e63c::getLoader();
這就是autoload.php 源碼了。
看來還得看看autoload_real.php
<?php// autoload_real.php @generated by Composerclass ComposerAutoloaderInit01f7a204137bd9325ebf450e4838e63c{ private static $loader; public static function loadClassLoader($class) { if ('Composer\Autoload\ClassLoader' === $class) { require __DIR__ . '/ClassLoader.php'; } } //首先被動用的方法 public static function getLoader() { if (null !== self::$loader) { return self::$loader; } //註冊自動載入函數,具體的用法可以看我的其他博文 spl_autoload_register(array('ComposerAutoloaderInit01f7a204137bd9325ebf450e4838e63c', 'loadClassLoader'), true, true); //實現自動載入類 的類 主要就是這個東西實現的自動載入 self::$loader = $loader = new \Composer\Autoload\ClassLoader(); //將之前自動載入函 刪除 spl_autoload_unregister(array('ComposerAutoloaderInit01f7a204137bd9325ebf450e4838e63c', 'loadClassLoader')); //主要根據環境的不同 啟用不同的自動載入方式 //PHP_VERSION_ID php的版本 //HHVM_VERSION 是Facebook 的一個為php提高效能的一個開源項目 可以自行瞭解 //是否使用了zend guard 的編碼方式 //有人想問為什麼要判斷這四項是嗎。 別問我 我tmd也不知道 往下看 $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); //雖然是兩個分支。不過最後都是給自動載入類,部署一些組態變數,為了讓自動載入的時候可以找到相應的檔案,把可以找到的檔案,按照規則配置到變數中。 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); } } //配置完變數了,就是那些你想用的類,然後就是註冊自動載入函數了。當執行個體化了不存在的類 就開始找類了,具體的代碼 貼在下面吧 $loader->register(true); //註冊架構必須用的一些類檔案 if ($useStaticLoader) { $includeFiles = Composer\Autoload\ComposerStaticInit01f7a204137bd9325ebf450e4838e63c::$files; } else { $includeFiles = require __DIR__ . '/autoload_files.php'; } foreach ($includeFiles as $fileIdentifier => $file) { composerRequire01f7a204137bd9325ebf450e4838e63c($fileIdentifier, $file); } return $loader; }}function composerRequire01f7a204137bd9325ebf450e4838e63c($fileIdentifier, $file){ if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { require $file; $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; }}
在來看看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 //根據之前配置的變數,在各個變數中尋找 //在classmap中找 if (isset($this->classMap[$class])) { return $this->classMap[$class]; } //在classMapAuthoritative中找 if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { return false; } //在apcuPrefix中找 if (null !== $this->apcuPrefix) { $file = apcu_fetch($this->apcuPrefix.$class, $hit); if ($hit) { return $file; } } //根據psr0 和 psr4 規則尋找具體的類檔案的位置 $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; }
其他部分的代碼不做說明了,不是很複雜,順著代碼 都能看懂。
總結:
在這幾個檔案中,主要的工作就是做一個可以自動載入類的工作,讓程式可以根據需要臨時載入想要的檔案。