搜索
首页开发工具composercomposer实现自动加载原理【详解】

下面由composer教程栏目给大家介绍composer实现自动加载原理,希望对需要的朋友有所帮助!

composer实现自动加载原理【详解】

简介

一般在框架中都会用到composer工具,用它来管理依赖。其中composer有类的自动加载机制,可以加载composer下载的库中的所有的类文件。那么composer的自动加载机制是怎么实现的呢?

composer 自动加载原理

以在Laravel框架中为例:

  1. 首先在入口文件(/public/index.php)中引入了autoload.php

    require __DIR__.'/../vendor/autoload.php';
  2. 我们看看autoload.php的内容

    require_once __DIR__ . '/composer/autoload_real.php';
    return ComposerAutoloaderInit1215780529014c2b50a6fca7ce889273::getLoader();
  3. 我们再看看 autoload_real.php的内容

    <?php
    
    // autoload_real.php @generated by Composer
    
    class ComposerAutoloaderInit1215780529014c2b50a6fca7ce889273
    {
        private static $loader;
    
        public static function loadClassLoader($class)
        {
            if (&#39;Composer\Autoload\ClassLoader&#39; === $class) {
                require __DIR__ . &#39;/ClassLoader.php&#39;;
            }
        }
    
        public static function getLoader()
        {
            if (null !== self::$loader) {
                return self::$loader;
            }
            spl_autoload_register(array(&#39;ComposerAutoloaderInit1215780529014c2b50a6fca7ce889273&#39;, &#39;loadClassLoader&#39;), true, true);
            self::$loader = $loader = new \Composer\Autoload\ClassLoader();
            spl_autoload_unregister(array(&#39;ComposerAutoloaderInit1215780529014c2b50a6fca7ce889273&#39;, &#39;loadClassLoader&#39;));
    
            $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined(&#39;HHVM_VERSION&#39;) && (!function_exists(&#39;zend_loader_file_encoded&#39;) || !zend_loader_file_encoded());
            if ($useStaticLoader) {
                require_once __DIR__ . &#39;/autoload_static.php&#39;;
    
                call_user_func(\Composer\Autoload\ComposerStaticInit1215780529014c2b50a6fca7ce889273::getInitializer($loader));
            } else {
                $map = require __DIR__ . &#39;/autoload_namespaces.php&#39;;
                foreach ($map as $namespace => $path) {
                    $loader->set($namespace, $path);
                }
    
                $map = require __DIR__ . &#39;/autoload_psr4.php&#39;;
                foreach ($map as $namespace => $path) {
                    $loader->setPsr4($namespace, $path);
                }
    
                $classMap = require __DIR__ . &#39;/autoload_classmap.php&#39;;
                if ($classMap) {
                    $loader->addClassMap($classMap);
                }
            }
    
            $loader->register(true);
    
            if ($useStaticLoader) {
                $includeFiles = Composer\Autoload\ComposerStaticInit1215780529014c2b50a6fca7ce889273::$files;
            } else {
                $includeFiles = require __DIR__ . &#39;/autoload_files.php&#39;;
            }
            foreach ($includeFiles as $fileIdentifier => $file) {
                composerRequire1215780529014c2b50a6fca7ce889273($fileIdentifier, $file);
            }
    
            return $loader;
        }
    }
    
    function composerRequire1215780529014c2b50a6fca7ce889273($fileIdentifier, $file)
    {
        if (empty($GLOBALS[&#39;__composer_autoload_files&#39;][$fileIdentifier])) {
            require $file;
    
            $GLOBALS[&#39;__composer_autoload_files&#39;][$fileIdentifier] = true;
        }
    }

可以看出这一段是composer自动加载的重点,首先在 autoload.php中调用ComposerAutoloaderInit1215780529014c2b50a6fca7ce889273::getLoader()方法,getLoader()首先判断当前\(loader是不是null,如果不为null就直接返回,否则就初始化一个ClassLoader类给赋值给\)loader,接着将autoload_namespaces.php、autoload_psr4.php、autoload_classmap.php文件中的内容加入到$loader中对应的数组中,然后给注册loadClass函数,将autoload_files.php中的所有路径所示的文件都包含进来,当在new一个类的时候如果没有找到相关的类就会触发这个loadClass函数,在loadClass()又调用了findFile()去查找相应的文件,找到相应文件后就会返回该文件,然后loadClass调用includeFile()方法将该文件include进去,否则findFile返回false,这样就完成了自动加载

  1. 下面来看一下 findFile()

     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, &#39;.php&#39;);
    
            // Search for Hack files if we are running on HHVM
            if (false === $file && defined(&#39;HHVM_VERSION&#39;)) {
                $file = $this->findFileWithExtension($class, &#39;.hh&#39;);
            }
    
            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;
        }
    
         private function findFileWithExtension($class, $ext)
        {
            // PSR-4 lookup
            $logicalPathPsr4 = strtr($class, &#39;\\&#39;, DIRECTORY_SEPARATOR) . $ext;
    
            $first = $class[0];
            if (isset($this->prefixLengthsPsr4[$first])) {
                $subPath = $class;
                while (false !== $lastPos = strrpos($subPath, &#39;\\&#39;)) {
                    $subPath = substr($subPath, 0, $lastPos);
                    $search = $subPath.&#39;\\&#39;;
                    if (isset($this->prefixDirsPsr4[$search])) {
                        $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
                        foreach ($this->prefixDirsPsr4[$search] as $dir) {
                            if (file_exists($file = $dir . $pathEnd)) {
                                return $file;
                            }
                        }
                    }
                }
            }
    
            // PSR-4 fallback dirs
            foreach ($this->fallbackDirsPsr4 as $dir) {
                if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
                    return $file;
                }
            }
    
            // PSR-0 lookup
            if (false !== $pos = strrpos($class, &#39;\\&#39;)) {
                // namespaced class name
                $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
                    . strtr(substr($logicalPathPsr4, $pos + 1), &#39;_&#39;, DIRECTORY_SEPARATOR);
            } else {
                // PEAR-like class name
                $logicalPathPsr0 = strtr($class, &#39;_&#39;, DIRECTORY_SEPARATOR) . $ext;
            }
    
            if (isset($this->prefixesPsr0[$first])) {
                foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
                    if (0 === strpos($class, $prefix)) {
                        foreach ($dirs as $dir) {
                            if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
                                return $file;
                            }
                        }
                    }
                }
            }
    
            // PSR-0 fallback dirs
            foreach ($this->fallbackDirsPsr0 as $dir) {
                if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
                    return $file;
                }
            }
    
            // PSR-0 include paths.
            if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
                return $file;
            }
    
            return false;
        }

    findFile()函数先在classMap中查找,如果找不到的话就会尝试在apcu缓存中查找,如果还是找不到的话就会调用findFileWithExtension()函数查找,如果找到了就会将该文件加到apcu缓存,如果找不到的话就会在missingClasses数组中设一个标记表示识这个类找不到

    findFileWithExtension()方法根据之前通过\(loader->set(\)namespace, \(path)和\)loader->setPsr4($namespace, $path)方法设置的信息找出类文件的路径信息

  2. 在上面有的地方用到了 spl_autoload_register和spl_autoload_unregister函数

    1. spl_autoload_register函数
      spl_autoload_register — 注册给定的函数作为 __autoload 的实现,
      1. bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]] )

      2. prepend
        如果是 true,spl_autoload_register() 会添加函数到队列之首,而不是队列尾部。

      3. 如果在你的程序中已经实现了__autoload()函数,它必须显式注册到__autoload()队列中。因为 spl_autoload_register()函数会将Zend Engine中的__autoload()函数取代为spl_autoload()或spl_autoload_call()
        例:

        function __autoload($name)
        {
            require &#39;class/&#39;.$name.&#39;.php&#39;;
            echo &#39;1&#39;;
        }
        function autoload_test($name)
        {
            echo &#39;2&#39;;
        }
        spl_autoload_register(&#39;autoload_test&#39;);
        spl_autoload_register(&#39;__autoload&#39;);
        $ca=new Ca();
    2. spl_autoload_unregister函数
      spl_autoload_unregister — 注销已注册的__autoload()函数,如果该函数队列处于激活状态,并且在给定函数注销后该队列变为空,则该函数队列将会变为无效。如果该函数注销后使得自动装载函数队列无效,即使存在有__autoload函数它也不会自动激活。
      1. bool spl_autoload_unregister ( mixed $autoload_function )

因本人水平有限,有些地方可能不对,欢迎留言。

以上是composer实现自动加载原理【详解】的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:cnblogs。如有侵权,请联系admin@php.cn删除
作曲家:通过AI的帮助开发PHP作曲家:通过AI的帮助开发PHPApr 29, 2025 am 12:27 AM

AI可以帮助优化Composer的使用,具体方法包括:1.依赖管理优化:AI分析依赖关系,建议最佳版本组合,减少冲突。2.自动化代码生成:AI生成符合最佳实践的composer.json文件。3.代码质量提升:AI检测潜在问题,提供优化建议,提高代码质量。这些方法通过机器学习和自然语言处理技术实现,帮助开发者提高效率和代码质量。

成为作曲家:培训,教育和经验成为作曲家:培训,教育和经验Apr 28, 2025 am 12:11 AM

要成为一名作曲家,需掌握音乐理论、和声学、对位法,并熟悉乐器音色和演奏技巧。作曲家通过音乐表达情感和故事,创作过程涉及从想法到作品的构建和完善。

确定作曲家:基本要素确定作曲家:基本要素Apr 27, 2025 am 12:27 AM

识别作曲家的关键步骤包括:1)分析作曲家的风格特征,如贝多芬的戏剧性和力量;2)了解作曲家的历史背景和文化影响,如巴赫的巴洛克时期风格;3)综合分析作品的旋律、和声、节奏和结构,避免仅依赖单一元素导致误判。

作曲家:PHP开发中AI的未来作曲家:PHP开发中AI的未来Apr 26, 2025 am 12:10 AM

Composer'sfutureinPHPdevelopmentwithAIincludes:1)AI-enhanceddependencymanagementforsuggestinglibraries,2)AI-drivencodegenerationfortailoredboilerplate,and3)predictivemaintenanceforupdatesandpatches,butfaceschallengeslikedataprivacyandAIbias.

作曲家的技能和素质:概述作曲家的技能和素质:概述Apr 25, 2025 am 12:03 AM

成为一名成功的作曲家需要具备音乐理论、乐器演奏和音响设计等技能,以及敏锐的灵感捕捉和不断的作品修改能力。作曲家通过这些技能和特质,将情感和思想转化为音乐作品,激发听众共鸣。

作曲家的资格:评估能力作曲家的资格:评估能力Apr 24, 2025 am 12:15 AM

Composer使用熟练程度可以通过以下四个方面评估:1)理解基础概念,如包、依赖和版本控制;2)掌握核心功能,包括解析composer.json、解决依赖、下载包和生成autoload文件;3)熟练使用基本和高级命令,如composerinstall、update、require和dump-autoload;4)应用最佳实践,如使用composer.lock文件、优化autoload配置和定期清理缓存。

作曲家:利用AI进行自动任务作曲家:利用AI进行自动任务Apr 23, 2025 am 12:03 AM

使用Composer结合AI可以实现自动化任务。1.Composer通过配置文件管理依赖,AI可优化版本选择。2.在实际应用中,AI可用于自动化依赖管理、测试和部署。3.性能优化包括依赖加载和缓存策略。4.需注意版本冲突和AI误判等问题。通过这些方法,AI能提升工作效率和智能化程度。

与AI:代码建议和见解的作曲家与AI:代码建议和见解的作曲家Apr 22, 2025 am 12:05 AM

ComposerwithAI是利用AI提升编程体验的工具。1)它通过分析代码结构、语法和模式,提供实时建议和错误修复。2)高级功能包括代码重构、性能优化和安全性检查。3)使用时可调整配置、提供反馈和结合其他工具来解决常见问题。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具