Laravel作為在國內(nèi)國外都頗為流行的html' target='_blank'>PHP框架,風(fēng)格優(yōu)雅,其擁有自己的一些特點。下面這篇文章主要給大家介紹了關(guān)于Laravel框架中composer自動加載實現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
基礎(chǔ)
自動加載允許你通過即用即加載的方式來加載需要的類文件,而不用每次都寫繁瑣的require 和include語句。因此,每一次請求的執(zhí)行過程都只加載必須的類,也不不要關(guān)心類的加載問題,只要需要的時候直接使用即可。
laravel 框架是通過composer 實現(xiàn)的自動加載。
是通過 下面的代碼實現(xiàn)的。
require_once __DIR__ . /composer . /autoload_real.php return ComposerAutoloaderInit7b20e4d61e2f88170fbbc44c70d38a1f::getLoader();
首先我們對spl_autoload_register和spl_autoload_unregister 這兩個函數(shù)進(jìn)行解釋一下。
spl_autoload_register 自動注冊 一個或多個 自動加載函數(shù),這些函數(shù)一般在 實例化類的時候,自動運行。
spl_autoload_unregister 恰恰相反。
貼上我實驗的代碼:
這是autoload.php
?php * Created by PhpStorm. * User: Administrator * Date: 2017/12/7 * Time: 14:10namespace app;class Autoload { public function __construct() $this- autoload(); public function autoload(){ // spl_autoload_register(array( Autoload , ss ),true); 會觸發(fā)致命錯誤,必須帶上命名空間 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:10require autoload.php $autoload=new /app/Autoload();$b=new B();// 此時自動運行自動加載函數(shù)echo 77;exit;
找到getLoader 這個函數(shù),并對其進(jìn)行分析:
public static function getLoader() if (null !== self::$loader) { return self::$loader; //注冊自動加載函數(shù),在加載或?qū)嵗悾\行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候,就是從加載的配置文件中尋找。尋找要加載的類主要通過register 函數(shù)來實現(xiàn)。然后分析register函數(shù)。
public function register($prepend = false) spl_autoload_register(array($this, loadClass ), true, $prepend);}
發(fā)現(xiàn)實際將該類中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);}
這段代碼其實就是加載autoload_file.php 文件。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,更多相關(guān)內(nèi)容請關(guān)注PHP !
相關(guān)推薦:
關(guān)于Laravel框架數(shù)據(jù)庫CURD操作和連貫操作的解析
PHP的Laravel框架中的event事件操作的解析
關(guān)于Laravel中的后期靜態(tài)綁定
以上就是如何實現(xiàn)Laravel框架中composer自動加載的詳細(xì)內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。
新聞熱點
疑難解答
圖片精選