下面由composer使用教程栏目为大家讲解Composer系列之autoload文件,希望对需要的朋友有所帮助!
简介
Composer 是 PHP 的一个依赖管理工具。它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们。
对于类库的自动加载,Composer 生成了一个 vendor/autoload.php 文件。引入这个文件,就能得到一个免费的自动加载支持,这种感觉真是棒极了~
本文主要是介绍如何利用 Composer 自带的自动加载机制,加载本地自定义的包(即不是来自于 packagist)。
使用
在 composer.json 文件中的 autoload 字段中添加自己的 autoloader。
目前 Composer 支持四种自动加载映射的方式:
PSR-0 autoloading PSR-4 autoloading classmap generation files
下面只介绍 PSR-4,为什么?因为它足够方便好用(当你添加类时不需要重新生成 autoloader)。详细请查看 自动加载 - 参考。
PSR-4(推荐)
{ "autoload": { "psr-4": { "First\\": "src/", "Temp\\": "lib/" } } }
上面的代码采用 PSR-4 规范,该规范包含了 PHP 最新的自动加载标准,它要求必须使用 namespace (命名空间)的方式。
First\\ 表示命名空间,必须以 \\ 结尾,避免相似的命名空间导致冲突,若包含子命名空间,可以这样表示:First\\Second\\。
src/ 表示命名空间所在目录为与 Composer 的 vendor 目录同级的 src 目录,如下所示:
First/ |---examples/ |---get.php |---src/ |---Curl.php |---vendor/ |---composer/ |---autoload.php |---composer.json
如果需要在多个目录下搜索相同的命名前缀,可以用一个数组提供:
{ "autoload": { "psr-4": {"First\\": ["src/", "lib/"]} } }
修改完 composer.json 的 autoload 字段后需要更新一下 Composer 的自动加载类:
composer dumpautoload -o
对应 Curl.php 的命名空间如下表示:
<?php namespace First; class Curl { }
引用 Curl.php 的方式如下:
<?php require '../vendor/autoload.php'; use First\Curl; $curl = new Curl(); ... ...
小结
之前一直以为 namespace 是 PHP 内置好的,只需要在某个类上声明一下某个命名空间,在调用这个类的地方 use 一下就好。。。
直到前几天脱离框架,单独写了几个类,然后被 xxx class not found 错误折磨到怀疑人生,才明白需要有个 autoloader 才行。
题外话
现在 Composer 已经成为 PHP 的依赖管理标杆,多利用这个工具为自己的开发带来方便。
框架能大大减少开发成本,但有时候不利于个人技术的提升。在使用热门框架时,多看看框架的代码,不仅帮助自己理解掌握框架,也能带来许多编程思想和技术上的提高。
The above is the detailed content of Detailed explanation of autoload in Composer series. For more information, please follow other related articles on the PHP Chinese website!

To become proficient when using Composer, you need to master the following skills: 1. Proficient in using composer.json and composer.lock files, 2. Understand how Composer works, 3. Master Composer's command line tools, 4. Understand basic and advanced usage, 5. Familiar with common errors and debugging techniques, 6. Optimize usage and follow best practices.

Composer is a dependency management tool for PHP, used to declare, download and manage project dependencies. 1) Declare dependencies through composer.json file, 2) Install dependencies using composerinstall command, 3) parse the dependency tree and download it from Packagist, 4) generate the autoload.php file to simplify automatic loading, 5) optimize use includes using composerupdate--prefer-dist and adjusting the autoload configuration.

AppComposer is a tool for building and managing applications. 1) It simplifies application development and improves efficiency by dragging and configuring predefined components. 2) Developers can define components, combine interfaces, define business logic, and ultimately render the application. 3) Support basic and advanced usage, such as task management and conditional rendering, helping to build flexible applications.

Composer is a dependency management tool for PHP. The core steps of using Composer include: 1) Declare dependencies in composer.json, such as "stripe/stripe-php":"^7.0"; 2) Run composerinstall to download and configure dependencies; 3) Manage versions and autoloads through composer.lock and autoload.php. Composer simplifies dependency management and improves project efficiency and maintainability.

ComposerAI is an artificial intelligence-based tool for generating and optimizing code to improve development efficiency and quality. Its functions include: 1. Code generation: generate code snippets that meet the standards according to requirements. 2. Code optimization: By analyzing existing code, make optimization suggestions. 3. Automated testing: Generate test cases to ensure code quality.

Composer is part of the SurfaceFlinger service in Android, and is responsible for synthesising multiple graphics layers into the final display buffer. 1) Collect the graphics layer, 2) sort the graphics layer, 3) synthesize the graphics layer, 4) output to the display device to improve application performance and user experience.

Composers are people who make music, express emotions, tell stories, and convey ideas through music. The composer's work includes: 1. Concept: determine the theme and style of the work; 2. Creation: compose melody and harmony to form a preliminary musical structure; 3. Experiment: audition and adjustment of the work through instruments or software; 4. Improvement: modify and improve according to the audition results until you are satisfied.

Composer is used to manage dependencies on PHP projects, while Orchestrator is used to manage and coordinate microservices or containerized applications. 1.Composer declares and manages dependencies of PHP projects through composer.json file. 2. Orchestrator manages the deployment and extension of services through configuration files (such as Kubernetes' YAML files), ensuring high availability and load balancing.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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