gpt4 book ai didi

未找到 PHP Composer Autoloader 类异常

转载 作者:可可西里 更新时间:2023-11-01 12:25:37 28 4
gpt4 key购买 nike

标题不言自明。所以这是我的项目结构:

|src
|Database
|Core
|MySQL.php
|Support
start.php
|vendor
composer.json
index.php

MySQL.php 文件:

<?php
namespace Database\Core;
//Some methods here

index.php 和 start.php 文件:

//start.php file
<?php
require __DIR__ . '/../vendor/autoload.php';
?>

//index.php file
<?php
use Database\Core;
require __DIR__ . '/src/start.php';

$mysql = new MySQL(); // Gets exception Class 'MySQL' cannot found etc.
?>

最后是我的 composer.json 自动加载部分:

"autoload": {
"psr-4": "Database\\": "src/" // Also tried "src/Database" too
}

问题出在哪里?我真的厌倦了尝试应对这种情况。请大家帮忙!谢谢。

最佳答案

初始化类时需要包含命名空间:

$mysql = new Database\Core\MySQL();

use Database\Core\MySQL;
$mysql = new MySQL();

参见 Using namespaces: Aliasing/Importing

关于未找到 PHP Composer Autoloader 类异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27554364/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com