1
votes

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?

1
You said you have the class Test.php inside Inc or you define in autoloader the namespace Inc\` for the folder inc`. Try to put it in uppercase.GrenierJ
Have you tried using the proper class name? Is there any class Test in your project, without any namespace, or did you import that namespace in your Index.php?Nico Haase

1 Answers

1
votes

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