Composer automatically loads instance analysis
通过在composer.json文件中配置需要加载的类、命名空间,通过执行composer install 命令自动生成类名和对应的类文件的映射,而后通过注册loadClass方法,实现对composer管理的诸多类的自动加载;
如何在composer.json文件中配置类和命名空间 ?
共有四种方式:
PSR-0(网上查到的例子和PSR-4没有看出太大区别,且已不推荐使用);
PSR-4;
Class-map;
Files;
在composer.json文件中添加以下代码块:
"autoload": { "psr-4": { "src\\darren\\": "src/", "project\\darren\\": "project" }, "files": ["common/Darren.php", "common/Since.php"], "classmamp": [lib] }
测试代码目录结构如下:
common Darren.php Since.php lib Darren.php Since.php project Darren.php src Darren.php vendor composer.json index.php
代码中的命名空间习惯为:目录名/Darren
当我们配置好composer.json文件,并执行compoer install
命令后,在vendor/composer目录下会自动生成一些php文件,这些文件实际上记录了类、命名空间和对应的类文件的映射,下面一一举例说明;
PSR-4
如上所述,通过psr-4方式配置了两个命名空间的自动加载,分别是src\daren和projecr\darren;vendor/composer目录下自动生成了autoload_psr4.php文件,具体代码如下所示:
<?php // autoload_psr4.php @generated by Composer $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( 'src\\darren\\' => array($baseDir . '/src'), 'project\\darren\\' => array($baseDir . '/project'), );
Classmap方式
classmap方式只需要我们配置需要自动加载的目录,compoer会自动扫描目录下的的.php文件或.inc文件中的class,并自动生成这些类和其对应的类文件的映射关系,保存在vendor/composer目录下的autoload_classmap.php文件中,具体代码如下:
<?php // autoload_classmap.php @generated by Composer $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( 'lib\\darren\\Darren' => $baseDir . '/lib/Darren.php', 'lib\\since\\Since' => $baseDir . '/lib/Since.php', );
其中lib\darren为命名空间,Darren为类名;
Files
files方式其实就是手动指定要加载的文件,这通常适用于一些全局的functions,可以将这些functions统一放在一个文件里,然后直接进行加载;
上述的配置文件通过files方式加载了两个文件common/Darren.php和common/Since.php,vendor/composer目录下自动生成了autoload_files.php文件,具体代码如下所示:
<?php // autoload_files.php @generated by Composer $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( 'b704865b506bf33e8097e6f62604fc7f' => $baseDir . '/common/Darren.php', '603921ee67f9053beb44a88f05b115d2' => $baseDir . '/common/Since.php', );
composer是如何实现自动加载的 ?
配置完compoer.json文件,跑完了composer install
命令,在文件的开始引用vendor/autolaod.php即可实现类的自动加载,那么composer是如何实现自动加载的呢?
这里先插叙一点php的特性:当调用不存在的类时,系统会自动调用__autoload( )方法来加载相应的类;
举例子说明如下:
我们在index.php文件中调用Darren类中的testAutoload( )方法【Darren类与index.php文件在同级目录】,这里我们没有在index.php文件中引入Darren类,那么肯定是会报错的;但是我们可以重写__autoload( )方法实现Darren类的加载,具体代码如下:
//当调用不存在的类时,系统自动调用__autolaod()查找 function __autolaod($class) { $file = $class . '.php'; if (is_file($file)) { require_once($file); } } Darren::testAutoload();
我们也可以通过spl_autoload_register( )方法来注册一个其它方法来替代__autoload( );
举例如下:
function loader($class) { $file = $class . '.php'; if (is_file($file)) { require_once($file); } } spl_autoload_register('loader'); Darren::testAutoload();
这样,loader方法就取代了__autoload;
接下来我们继续研究composer背后的加载机制,autoload.php中引入了autoload_real.php然后调用了getLoader( )方法,getLoader( )方法具体代码如下:
public static function getLoader() { if (null !== self::$loader) { return self::$loader; } spl_autoload_register(array('ComposerAutoloaderInit0a8197b9e4da93df051721eff8ed7b28', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(); spl_autoload_unregister(array('ComposerAutoloaderInit0a8197b9e4da93df051721eff8ed7b28', 'loadClassLoader')); $map = require __DIR__ . '/autoload_namespaces.php'; foreach ($map as $namespace => $path) { $loader->set($namespace, $path); } //整理通过psr-4方式配置的类 $map = require __DIR__ . '/autoload_psr4.php'; foreach ($map as $namespace => $path) { $loader->setPsr4($namespace, $path); } //整理通过classmap方式配置的类 $classMap = require __DIR__ . '/autoload_classmap.php'; if ($classMap) { $loader->addClassMap($classMap); } //注册实现自动加载的方法 $loader->register(true); //直接引入通过files配置的类 $includeFiles = require __DIR__ . '/autoload_files.php'; foreach ($includeFiles as $fileIdentifier => $file) { composerRequire0a8197b9e4da93df051721eff8ed7b28($fileIdentifier, $file); } return $loader; }
该方法先是创建了一个ClassLoader类的对象,然后加载composer自动生成的那些记录类、命名空间与文件映射关系的文件,调用ClassLoader中的方法对这些文件整理,并将映射关系通过数组保存,数组的键为类名或者命名空间加类名,数组的值为类对应的类文件地址;这里值得注意的一点是,我们通过files配置的那些需要自动加载的类,是直接将类文件引入进来,并不是在调用时才去加载,代码如下:
function composerRequire0a8197b9e4da93df051721eff8ed7b28($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { require $file;//直接引入类文件 $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; } }
ClassLoader类中值得注意的方法:
通过register方法注册loadClass方法,取代__autoload( )方法,实现类的加载
public function register($prepend = false) { spl_autoload_register(array($this, 'loadClass'), true, $prepend); }
loadClass方法查找类对应的文件,并引入:
public function loadClass($class) { if ($file = $this->findFile($class)) { includeFile($file); return true; } }
具体如何查找数组从而得到该类对应的类文件,可以通过xdebug跟一遍代码,并不难理解;
相关推荐:
Thinkphp5 使用composer中seeder播种机
The above is the detailed content of Composer automatically loads instance analysis. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

I'm having a tricky problem when doing a mail marketing campaign: how to efficiently create and send mail in HTML format. The traditional approach is to write code manually and send emails using an SMTP server, but this is not only time consuming, but also error-prone. After trying multiple solutions, I discovered DUWA.io, a simple and easy-to-use RESTAPI that helps me create and send HTML mail quickly. To further simplify the development process, I decided to use Composer to install and manage DUWA.io's PHP library - captaindoe/duwa.

I had a tough problem when working on a project with a large number of Doctrine entities: Every time the entity is serialized and deserialized, the performance becomes very inefficient, resulting in a significant increase in system response time. I've tried multiple optimization methods, but it doesn't work well. Fortunately, by using sidus/doctrine-serializer-bundle, I successfully solved this problem, significantly improving the performance of the project.

When developing an e-commerce platform, it is crucial to choose the right framework and tools. Recently, when I was trying to build a feature-rich e-commerce website, I encountered a difficult problem: how to quickly build a scalable and fully functional e-commerce platform. I tried multiple solutions and ended up choosing Fecmall's advanced project template (fecmall/fbbcbase-app-advanced). By using Composer, this process becomes very simple and efficient. Composer can be learned through the following address: Learning address

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.
