PHP中使用Composer时的自动加载和命名空间问题

huangapple go评论68阅读模式
英文:

Issue with Autoloading and Namespace in PHP using Composer

问题

我正在遇到理解PHP中命名空间的概念的困难。我应该设置如下的composer.json文件:

{
    "name": "test/test-namespace",
    "autoload": {
        "psr-4": {
            "Test\\TestNamespace\\": "src/",
            "Test\\TestNamespace\\A\\": "src/A/",
            "Test\\TestNamespace\\B\\": "src/B/",
            "Test\\TestNamespace\\C\\": "src/C/"
        }
    },
    "authors": [],
    "require": {}
}

我的目录结构如下:

.
├── composer.json
├── index.php
├── src
│   ├── A
│   │   └── a.php
│   ├── B
│   │   └── b.php
│   └── C
│       └── c.php
└── vendor
    └── autoload.php

这是a.php文件的内容,它位于/A文件夹中:

<?php
namespace Test\TestNamespace\A;

class Test_A {
    public function __construct()
    {
        echo 'Test_A';
    }
}

现在,我尝试使用命名空间调用这些类,代码如下:

<?php
require_once __DIR__ . "/vendor/autoload.php";

use Test\TestNamespace\A\Test_A;
use Test\TestNamespace\B\Test_B;
use Test\TestNamespace\C\Test_C;

$a = new Test_A();
$b = new Test_B();

然而,我遇到以下错误消息,我不确定如何解决这个问题:

php index.php         
PHP Fatal error:  Uncaught Error: Class "Test\TestNamespace\A\Test_A" not found in .../PhpstormProjects/test-namespace/index.php:8
Stack trace:
#0 {main}
  thrown in .../PhpstormProjects/test-namespace/index.php on line 8

Fatal error: Uncaught Error: Class "Test\TestNamespace\A\Test_A" not found in .../PhpstormProjects/test-namespace/index.php:8
Stack trace:
#0 {main}
  thrown in .../PhpstormProjects/test-namespace/index.php on line 8

看起来找不到这些类,我不确定如何解决这个问题。

英文:

I'm having difficulty understanding the concept of namespacing in PHP. I should have set

{
   &quot;name&quot;: &quot;test/test-namespace&quot;,
   &quot;autoload&quot;: {
       &quot;psr-4&quot;: {
           &quot;Test\\TestNamespace\\&quot;: &quot;src/&quot;,
           &quot;Test\\TestNamespace\\A\\&quot;: &quot;src/A/&quot;,
           &quot;Test\\TestNamespace\\B\\&quot;: &quot;src/B/&quot;,
           &quot;Test\\TestNamespace\\C\\&quot;: &quot;src/C/&quot;
       }
   },
   &quot;authors&quot;: [],
   &quot;require&quot;: {}
}

My directory structure looks like this:

.
├── composer.json
├── index.php
├── src
│&#160;&#160; ├── A
│&#160;&#160; │&#160;&#160; └── a.php
│&#160;&#160; ├── B
│&#160;&#160; │&#160;&#160; └── b.php
│&#160;&#160; └── C
│&#160;&#160;     └── c.php
└── vendor
    ├── autoload.php

As an example, here's the contents of a.php, which is located in the /A folder:

&lt;?php
namespace Test\TestNamespace\A;


class Test_A {
    public function __construct()
    {
        echo &#39;Test_A&#39;;
    }
}

Now, I'm trying to call the classes using namespaces as follows:

&lt;?php
require_once __DIR__ . &quot;/vendor/autoload.php&quot;;

use Test\TestNamespace\A\Test_A;
use Test\TestNamespace\B\Test_B;
use Test\TestNamespace\C\Test_C;

$a = new Test_A();
$b = new Test_B();

However, I'm getting the following error message and I'm not sure how to solve the issue:

php index.php         
PHP Fatal error:  Uncaught Error: Class &quot;Test\TestNamespace\A\Test_A&quot; not found in .../PhpstormProjects/test-namespace/index.php:8
Stack trace:
#0 {main}
  thrown in .../PhpstormProjects/test-namespace/index.php on line 8

Fatal error: Uncaught Error: Class &quot;Test\TestNamespace\A\Test_A&quot; not found in .../PhpstormProjects/test-namespace/index.php:8
Stack trace:
#0 {main}
  thrown in .../PhpstormProjects/test-namespace/index.php on line 8

It seems that the classes can't be found, and I'm unsure how to resolve this issue.

答案1

得分: -1

根据PSR-4标准,你的类名必须与文件名匹配,以便实现自动加载。

根据你的示例,你应该将Test_A类的文件名设置为Test_A.php。

此外,你不需要在psr-4映射下添加子目录。一旦你添加了顶级目录,目录A下的每个类都将具有以下命名空间:

Test\TestNamespace\A\Test_A将位于src/A/目录下。

所以你只需要添加第一个映射即可。

英文:

According to PSR-4 standards, your class names must match the names of the files for autoloading to work.

From your example, you're supposed to have Test_A class file name as Test_A.php.

Also, you don't need to add sub directories under the psr-4 maps. Once you add the top directory, every class under directory A will have the following namespace:

Class Test\TestNamespace\A\Test_A will be in directory src/A/

So all you really need to do is have the first map.

huangapple
  • 本文由 发表于 2023年8月8日 23:18:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定