Laravel框架中composer自動加載的實(shí)現(xiàn)分析
基礎(chǔ)
自動加載允許你通過即用即加載的方式來加載需要的類文件,而不用每次都寫繁瑣的require 和include語句。因此,每一次請求的執(zhí)行過程都只加載必須的類,也不不要關(guān)心類的加載問題,只要需要的時(shí)候直接使用即可。
laravel 框架是通過composer 實(shí)現(xiàn)的自動加載。
是通過 下面的代碼實(shí)現(xiàn)的。
require_once __DIR__ . '/composer' . '/autoload_real.php'; return ComposerAutoloaderInit7b20e4d61e2f88170fbbc44c70d38a1f::getLoader();
首先我們對spl_autoload_register和spl_autoload_unregister 這兩個(gè)函數(shù)進(jìn)行解釋一下。
spl_autoload_register 自動注冊 一個(gè)或多個(gè) 自動加載函數(shù),這些函數(shù)一般在 實(shí)例化類的時(shí)候,自動運(yùn)行。
spl_autoload_unregister 恰恰相反。
貼上我實(shí)驗(yàn)的代碼:
這是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); 會觸發(fā)致命錯(cuò)誤,必須帶上命名空間 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();// 此時(shí)自動運(yùn)行自動加載函數(shù) echo 77; exit;
找到getLoader 這個(gè)函數(shù),并對其進(jìn)行分析:
public static function getLoader() { if (null !== self::$loader) { return self::$loader; } //注冊自動加載函數(shù),在加載或?qū)嵗悾\(yùn)行l(wèi)oadClassLoader函數(shù) 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 等屬性進(jìn)行賦值。即加載一些配置好的文件,在后面進(jìn)行加載或?qū)ふ椅募r(shí)候,就是從加載的配置文件中尋找。尋找要加載的類主要通過register 函數(shù)來實(shí)現(xiàn)。然后分析register函數(shù)。
public function register($prepend = false) { spl_autoload_register(array($this, 'loadClass'), true, $prepend); }
發(fā)現(xiàn)實(shí)際將該類中l(wèi)oadClass 函數(shù)注冊為自動加載函數(shù)。于是開始分析loadClass函數(shù),最終是通過findFile進(jìn)行類的尋找。
public function findFile($class) { /// 特別注意 參數(shù)$class 是根據(jù)命名空間生成的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 規(guī)則尋找,再按照psr0 尋找 // 兩種規(guī)則的不同主要是對下劃線的處理方式。 $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函數(shù)分析完。我們接著分析getLoader函數(shù)剩余代碼。
$includeFiles = require __DIR__ . '/autoload_files.php'; foreach ($includeFiles as $fileIdentifier => $file) { composerRequire7b20e4d61e2f88170fbbc44c70d38a1f($fileIdentifier, $file); }
這段代碼其實(shí)就是加載autoload_file.php 文件。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- PHP管理依賴(dependency)關(guān)系工具 Composer的自動加載(autoload)
- Laravel 解決composer相關(guān)操作提示php相關(guān)異常的問題
- laravel 實(shí)現(xiàn)向公共模板中傳值 (view composer)
- 淺談laravel 5.6 安裝 windows上使用composer的安裝過程
- 使用composer 安裝 laravel框架的方法圖文詳解
- 一次因composer錯(cuò)誤使用引發(fā)的問題與解決
- tp5框架使用composer實(shí)現(xiàn)日志記錄功能示例
- windows環(huán)境下使用Composer安裝ThinkPHP5
- PHP創(chuàng)建自己的Composer包方法
- 分析Composer實(shí)現(xiàn)自動加載原理
相關(guān)文章
laravel框架創(chuàng)建授權(quán)策略實(shí)例分析
這篇文章主要介紹了laravel框架創(chuàng)建授權(quán)策略,結(jié)合實(shí)例形式分析了laravel框架創(chuàng)建授權(quán)策略的相關(guān)步驟、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2019-11-11php根據(jù)身份證號碼計(jì)算年齡的實(shí)例代碼
我們只要知道身份證的生成規(guī)則就可以了,像下面我們從指定位置到多少位就是出日期了,然后我們把日期轉(zhuǎn)成時(shí)間戳然后進(jìn)行加減運(yùn)算就得出了年齡了,下面我們看實(shí)例2014-01-01thinkPHP5項(xiàng)目中實(shí)現(xiàn)QQ第三方登錄功能
這篇文章主要介紹了thinkPHP5項(xiàng)目中實(shí)現(xiàn)QQ第三方登錄功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了修改QQ登陸接口并整合進(jìn)thinkPHP5項(xiàng)目中的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Redis在Laravel項(xiàng)目中的應(yīng)用實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于Redis在Laravel項(xiàng)目中應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用laravel具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08ThinkPHP做文字水印時(shí)提示call an undefined function exif_imagetype()解
這篇文章主要介紹了ThinkPHP做文字水印時(shí)提示call an undefined function exif_imagetype()解決方法,是項(xiàng)目開發(fā)中非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10laravel郵件發(fā)送的實(shí)現(xiàn)代碼示例
這篇文章主要介紹了laravel郵件發(fā)送的實(shí)現(xiàn)代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01php+ajax+h5實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了php+ajax+h5實(shí)現(xiàn)ajax圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10在TP5數(shù)據(jù)庫中四個(gè)字段實(shí)現(xiàn)無限分類的示例
今天小編就為大家分享一篇在TP5數(shù)據(jù)庫中四個(gè)字段實(shí)現(xiàn)無限分類的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10