Home  >  Article  >  Development Tools  >  Will you use composer to add automatic loading function to your own class library?

Will you use composer to add automatic loading function to your own class library?

藏色散人
藏色散人forward
2020-07-18 13:13:012954browse

下面由composer教程栏目给大家介绍用composer给自己的类库加上自动加载功能的方法,希望对需要的朋友有所帮助!

Will you use composer to add automatic loading function to your own class library?

最近在搞api架构设计,要与之前的代码隔离开来,之前的代码种种原因没有不是用的类似的自动加载,用了composer发现真的做的非常方便,自动加载功能及其好用,所以在想能不能直接利用composer的自动加载来为自己写的类库提供自动加载功能!

百度谷歌StackOverflow,该有的答案总是会有的!

假设你已经会composer的基本使用。

方法:

在composer.json中添加类似代码:

    "autoload": { //自动加载
        "psr-4": { //申明遵循psr-4规范
            "api\\": "api" // namespace(根): 文件夹映射目录(基于composer.json所在文件夹)
        }
    }

到composer.json所在目录,运行:

$ composer dump-autoload

之后你就可以使用composer的类自动加载自己的类库了。

The above is the detailed content of Will you use composer to add automatic loading function to your own class library?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete