php 作曲家自动加载器 psr-0 命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18146057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:03:45  来源:igfitidea点击:

composer autoloader psr-0 namespaces

phpnamespacescomposer-phpautoloadpsr-0

提问by brpaz

I have create a custom composer package but I am having troubles to set the correct autoload options for it.

我创建了一个自定义 Composer 包,但无法为其设置正确的自动加载选项。

All my classes are under MyNamespace/Commonnamespace. So for example for including my ArrayHelperclass I do use Mynamespace/Common/Helper/ArrayHelper.

我所有的类都在MyNamespace/Common命名空间下。因此,例如为了包括我的ArrayHelper课程,我确实使用Mynamespace/Common/Helper/ArrayHelper.

This is the relevant part of my composer.json:

这是我的相关部分composer.json

"autoload": {
    "psr-0": { "MyNamespace\": "" }
} 

I have read this: composer.json / autoload

我读过这个:composer.json / autoload

Any help?

有什么帮助吗?

回答by KarelG

You have to navigate the file location of your namespace.

您必须导航命名空间的文件位置。

"autoload": {
    "psr-0": { "MyNameSpace": "./<path to your parent directory>" }
}

For example, this is my directory structure:

例如,这是我的目录结构:

composer.json
source
  \-Data
    |-Controller
    \-Repository

Then, in the composer.json file:

然后,在 composer.json 文件中:

"autoload": {
    "psr-0": { "MyNameSpace": "source/Data" }
}

Then, I can call the namespace from PHP by

然后,我可以从 PHP 调用命名空间

/* namespace for classes in controller directory */
namespace MyNameSpace\Controller
/* namespace for classes in repository directory */
namespace MyNameSpace\Repository