英文:
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
{
"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": {}
}
My directory structure looks like this:
.
├── composer.json
├── index.php
├── src
│   ├── A
│   │   └── a.php
│   ├── B
│   │   └── b.php
│   └── C
│   └── c.php
└── vendor
├── autoload.php
As an example, here's the contents of a.php, which is located in the /A folder:
<?php
namespace Test\TestNamespace\A;
class Test_A {
public function __construct()
{
echo 'Test_A';
}
}
Now, I'm trying to call the classes using namespaces as follows:
<?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();
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 "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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论