Xenonite
Xenonite

Reputation: 1957

composer autoload wont work

I have just started a new PHP project and right up front, I am running into issues with the autoloader. I searched for the error and consulted the documentation (http://www.php-fig.org/psr/psr-4/), but the issue persists.

Hence, I created a minimal example to narrow down the cause of the error - yet, even with this minimal example, it wont work :(

My folder structure is like this:

+ src/
| + Xyz.php
+ composer.json
+ test.php

Here is my code

composer.json:

{
    "name": "sg/ABC",
    "description": "abc",
    "autoload": {
        "psr-4": {
            "sg\\ABC\\": "src/"
        }
    }
}

Xyz.php:

<?php namespace sg\ABC;
    class Xyz
    {}
?>

test.php:

<?php namespace sg\ABC;
    use sg\ABC\Xyz;
    $a = new Xyz();
?>

Even though running composer install shows no errors, I immediately get this error when running the code:

$ php test.php 
PHP Fatal error:  Class 'sg\ABC\Xyz' not found in /dir/x/test.php on line 5

Fatal error: Class 'sg\ABC\Xyz' not found in /dir/x/test.php on line 5

also, running composer dump-autoload (as suggested here and there in this board) does not help

Upvotes: 0

Views: 86

Answers (2)

user4301733
user4301733

Reputation:

You need to require the autoloader .. it is usually require_once "path/to/vendor/autoload.php".

Upvotes: 1

Jerodev
Jerodev

Reputation: 33216

You still need to include the composer autoload.php file to include the loaded libraries.

Upvotes: 1

Related Questions