[英]Class not found in psr-4 composer autoloading
I have a base folder with a vendor folder which include composer folder and my created folder src. 我有一个包含供应商文件夹的基本文件夹,其中包括composer文件夹和我创建的文件夹src。 The src folder contain all classes required to work.
src文件夹包含工作所需的所有类。
In src folder, I have class example below 在src文件夹中,我有下面的类示例
<?php
namespace Test;
class Example{
public function display(){
echo "This is Example Class";
}
}
?>
my composer.json has following content: 我的composer.json具有以下内容:
{
"autoload":{
"psr-4":{
"Test\\":"src"
}
}
}
and finally I am creating the instance of example class in a test file in base folder: 最后,我在基本文件夹的测试文件中创建了示例类的实例:
<?php
require 'vendor/autoload.php';
$a=new Test\Example();
$a->display();
?>
The issue is when i try to run test file the following error occurred: [Fatal error: Uncaught Error: Class 'Test\\Example' not found.] 问题是当我尝试运行测试文件时发生以下错误: [致命错误:未捕获的错误:未找到类'Test \\ Example'。]
Please help me what I am doing wrong, Here is the screen shot of directory structure. 请帮助我我做错了什么, 这是目录结构的屏幕快照。 Thanks in advance.
提前致谢。
To be honest I'm not sure where your src
, testing.php
and composer.json
are located, but they should be located in main directory (in your case 2-Basic-example and not in vendor
directory). 老实说,我不确定您的
src
, testing.php
和composer.json
的位置,但是它们应该位于主目录中(在您的情况下为2-Basic-example,而在vendor
目录中)。 Then just run: 然后运行:
composer dump-autoload
and it should work assuming your Example
class is saved as Example.php
in src
directory 假设您的
Example
类另存为src
目录中的Example.php
,它应该可以工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.