How does PSR-4 autoloading work in composer for custom libraries?

后端 未结 1 758
北恋
北恋 2021-01-17 19:27

I use the following directory structure based on my understanding of how namespaces in PHP work:

project_root
    app/
    |    lib/
    |    |    MyCompany/         


        
相关标签:
1条回答
  • 2021-01-17 20:21

    Taken from the documentation you linked:

    {
        "autoload": {
            "psr-4": {
                "MyCompany\\": "app/lib/MyCompany/",
            }
        }
    }
    

    This is pretty self explanatory, it simply tells the autoloader that app/lib/MyCompany is the root for the MyCompany\ namespace.

    You would then be able to use the class as \MyCompany\Utility\Logger.

    Note that in PSR-4, unlike PSR-0, you'd normally omit MyCompany from the directory structure, and just use app/lib/.

    0 讨论(0)
提交回复
热议问题