Lord Razen
Lord Razen

Reputation: 21

Issue with Composer Autoload

I'm kinda new to Autoload Composer, but hopefully I got the composer concept right: Basically you hand over some basic information (composer.json) and the composer then generates the classes which you have to include and then do the work you wanted. Right?

Well, after hours of trying to setup an Autoload Composer, that's my result:

Composer.json:

    "name": "lordrazen/dpgenerator",
    "description": "Page to generate Datapacks out of Rawfiles",
    "type": "project",
    "license": "GPL",
    "authors": [
        {
            "name": "LordRazen",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "psr-4": {"Inc\\": "inc/"}
    }
}

Class "Test.php" inside the folder "Inc"

namespace Inc;

class Test {

}

Index.php

if (file_exists(dirname(__FILE__) . '/vendor/autoload.php')) {
        require_once (dirname(__FILE__) . '/vendor/autoload.php');
    }

    $test = new Test();

I also went to the projects directory in the console and hit "composer update" command.

Well... it still doesnt work:

Fatal error: Uncaught Error: Class 'Test' not found in [...]/index.php:34 Stack trace: #0 {main} thrown in [...]/index.php on line 34

What's my mistake here?

Upvotes: 1

Views: 626

Answers (1)

Jens A. Koch
Jens A. Koch

Reputation: 41756

You have a class Test in a namespace Inc, but you are not using it, when you are instantiating the class. Just append it in the root folder index.php, like so:

<?php

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

$test = new \Inc\Test();

Upvotes: 1

Related Questions