search
HomeDevelopment ToolscomposerIntroducing four methods of composer automatic loading

下面由composer教程栏目给大家介绍composer自动载入的四种方式 ,希望对需要的朋友有所帮助!

Introducing four methods of composer automatic loading

composer自动载入的四种方式

composer 是PHP的包依赖管理工具,类似于node.js的npm 。并非官方,但已经十分流行。

对于第三方包的自动加载,composer 提供里四种支持。分别是psr-0,psr-4,生成class-map,直接包含files

psr-0 自动加载

psr-1 基本代码规范

psr-2 代码样式规范

psr-3 日志接口

psr-4 自动加载

psr-0 与 psr-4的主要区别是 psr-4中下划线不做任何处理。psr-0中下划线会转义为目录分隔符

在主文件 index.php中编写

require 'vendor/autoload.php';

1.PSR-4

在composer.json中是这样配置的

{
  "aotuload":{
        "psr-4":{
            "FOO\\":"src/"
     }
  }
}

当在index.php中new FOO\A\A 这个类时。composer会去查找src/A/A.php.如果这位文件存在,则进行加载

2.PSR-0

在composer.json中是这样配置的

{
  "aotuload":{
        "psr-0":{
            "FOO\\":"src/"
     }
  }
}

当在index.php中new FOO\A\A 这个类时。composer会去查找src/FOO/A/A.php.如果这位文件存在,则进行加载

3.class-map

{
  "aotuload":{
       "class-map":['a/','b/','c/']
    }
}

composer 会扫描abc 目录下以.php或.inc结尾的class.并生产新的文件映射关系,放到/vendor/composer/aotuload_classmap

4.files

{
   "aotuload":{
       "files":["src/my/function.php"]
 }
}

手动指定供提供加载的文件。如全局的function文件

The above is the detailed content of Introducing four methods of composer automatic loading. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:jianshu. If there is any infringement, please contact admin@php.cn delete
Composer and AI: New Possibilities in PHP DevelopmentComposer and AI: New Possibilities in PHP DevelopmentApr 19, 2025 am 12:03 AM

The combination of AI and Composer can improve PHP development efficiency and security. Specifically reflected in: 1. Dependency analysis and optimization: AI can predict dependencies and reduce conflicts. 2. Automated security checks: AI can identify security vulnerabilities, and it is recommended to update them. 3. Code generation and optimization: AI can automatically generate and optimize related code.

Using Dicr/Yii2-Google to integrate Google API in YII2Using Dicr/Yii2-Google to integrate Google API in YII2Apr 18, 2025 am 11:54 AM

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

How to use Composer to resolve JSON Schema verification issuesHow to use Composer to resolve JSON Schema verification issuesApr 18, 2025 am 11:51 AM

I'm having a tricky problem when developing a Symfony-based application: how to effectively validate JSON data format. Initially, I tried using manual verification code, but this was not only complicated, but also error-prone. After some exploration, I discovered a Composer package called ptyhard/json-schema-bundle, which brought great convenience and efficiency to my project.

Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundleUse Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundleApr 18, 2025 am 11:48 AM

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:

How to quickly build Fecmall advanced project templates using ComposerHow to quickly build Fecmall advanced project templates using ComposerApr 18, 2025 am 11:45 AM

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

Improve Doctrine entity serialization efficiency: application of sidus/doctrine-serializer-bundleImprove Doctrine entity serialization efficiency: application of sidus/doctrine-serializer-bundleApr 18, 2025 am 11:42 AM

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.

Use Composer to resolve error logging issues in Laravel projectsUse Composer to resolve error logging issues in Laravel projectsApr 18, 2025 am 11:39 AM

When developing Laravel projects, the management of error logs is a very critical link. Recently, I encountered a problem in my project: how to efficiently capture and record all types of errors and ensure that these error messages can be processed in a timely manner. After some research, I found the lukeboy25/errorlogger package. It is installed through Composer and can greatly simplify the management process of error logs. You can learn composer through the following address:

How to use Composer to improve the security of Laravel applications: Applications of wiebenieuwenhuis/laravel-2fa libraryHow to use Composer to improve the security of Laravel applications: Applications of wiebenieuwenhuis/laravel-2fa libraryApr 18, 2025 am 11:36 AM

When developing a Laravel application, I encountered a common but difficult problem: how to improve the security of user accounts. With the increasing complexity of cyber attacks, a single password protection is no longer enough to ensure the security of users' data. I tried several methods, but the results were not satisfactory. Finally, I installed the wiebenieuwenhuis/laravel-2fa library through Composer and successfully added two-factor authentication (2FA) to my application, greatly improving security.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function