1

I'm getting this error...

Fatal error: Uncaught Error: Class 'TestApp\HelloWorld' not found in /Applications/MAMP/htdocs/php-framework/index.php:21 Stack trace: #0 {main} thrown in /Applications/MAMP/htdocs/php-framework/index.php on line 21

My guess is that composer.json is not connecting to the src folder, but not sure what's causing it. I'm using PHP 7.3.1

Here are my files:

index.php

declare(strict_types = 1);

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

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

autoload.php

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

return ComposerAutoloaderInita3c559aaa188cb577ee6600701c362e7::getLoader();

HelloWorld.php

declare(strict_types = 1);

namespace TestApp;

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

composer.json

{
    "name": "philginsburg/php-framework",
    "description": "An example of a modern PHP application.",
    "type": "project",
    "require": {},
    "autoload": {
        "psr-4": {
            "TestApp\\": "src/"
        }
    }
}

Fatal error: Uncaught Error: Class 'TestApp\HelloWorld' not found in /Applications/MAMP/htdocs/php-framework/index.php:21 Stack trace: #0 {main} thrown in /Applications/MAMP/htdocs/php-framework/index.php on line 21

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Ginz77
  • 63
  • 3
  • 10

2 Answers2

0

Using your helloworld.php and composer.json, following should help you on your way:

project structure:

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();

output: Hello World!

lovelace
  • 1,195
  • 1
  • 7
  • 10
  • I was still getting issues and it seems that the composer.json was cached. Using the command `composer dumpautoload` fixed the problem. Thank you for your help. – Ginz77 Jun 27 '19 at 04:01
0

You should run command "composer update".