致命错误:未捕获的错误:作曲家自动加载

时间:2019-06-26 21:18:11

标签: php composer-php php-7.3

我遇到此错误...

  

致命错误:未捕获错误:在/Applications/MAMP/htdocs/php-framework/index.php中找不到类'TestApp \ HelloWorld':21堆栈跟踪:#0 {main}抛出在/ Applications / MAMP / htdocs中/php-framework/index.php在第21行

我的猜测是composer.json没有连接到src文件夹,但是不确定是什么原因造成的。我正在使用PHP 7.3.1

这是我的文件:

index.php

pictureBox1.Location.Translate(offset, offset);

autoload.php

declare(strict_types = 1);

require_once __DIR__ . '/vendor/autoload.php';

$helloWorld = new TestApp\HelloWorld();
$helloWorld->announce();

HelloWorld.php

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInita3c559aaa188cb577ee6600701c362e7::getLoader();

composer.json

declare(strict_types = 1);

namespace TestApp;

class HelloWorld
{
    public function announce(): void
    {
        echo 'Hello World!';
    }
}

致命错误:未捕获错误:在/Applications/MAMP/htdocs/php-framework/index.php中找不到类'TestApp \ HelloWorld':21堆栈跟踪:#0 {main}抛出在/ Applications / MAMP / htdocs中/php-framework/index.php在第21行

2 个答案:

答案 0 :(得分:0)

使用您的helloworld.php和composer.json,以下内容将对您有所帮助:

项目结构:

enter image description here

index.php

<?php
declare(strict_types = 1);

use TestApp\HelloWorld;

require dirname(__DIR__, 1) . '/vendor/autoload.php';

$helloWorld = new HelloWorld();
$helloWorld->announce();

输出:Hello World!

答案 1 :(得分:0)

您应该运行命令“ composer update”。