Introducing four methods of composer automatic loading

藏色散人
Release: 2020-09-08 09:28:50
forward
2764 people have browsed it

下面由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';
Copy after login

1.PSR-4

在composer.json中是这样配置的

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

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

2.PSR-0

在composer.json中是这样配置的

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

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

3.class-map

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

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

4.files

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

手动指定供提供加载的文件。如全局的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!

Related labels:
source:jianshu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!