D
D
Dmitry Bezmenov2019-11-29 18:27:01
PHP
Dmitry Bezmenov, 2019-11-29 18:27:01

Why is the composer autoloader not working?

There is a package packaged for composer (installed from the repository) with the following structure:
myvendor/
----import/
--------src/
------------Xml/
--- -------------File.php
--------composer.lock
--------composer.json

Composer.json file content

{
    "name": "myvendor/import",
    "description": "description",
    "type": "library",
    "license": "MIT",
    "require": {
        "php": ">=5.6"
    },
    "require-dev": {
        "phpunit/phpunit": "^8.4"
    },
    "autoload": {
        "psr-4": { 
            "Import\\": "src/" 
        }
    }
}


File.php content

<?php
namespace Import\Xml;

class File
{
    //...
}


This package is used at the root of the project in the import.php file
Sample contents of the import.php file

<?php

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

use Import\Xml;

$file = new Xml\File();

//...


Sample contents of the composer.json file at the root of the project

{
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "myvendor/import",
                "version": "1.0.0",
                "source": {
                    "url": "...",
                    "type": "git",
                    "reference": "master"
                }
            }
        }
    ],
    "require": {
        "myvendor/import": "^1.0"
    }
}


An attempt to interpret the import.php file ends up with the following error message:
Fatal error: Uncaught Error: Class 'Import\Xml\File' not found in ...

"composer dump" returns: Interesting - adding a standard loader
Generated autoload files containing 0 classes
to import.php fixes this error Tested on: 1) Windows 7, Composer 1.9.0, PHP 7.2.23 2) CentOS 6.9, Composer 1.5.2, PHP 5.6.33 Based on everything tested, I can conclude that the error is somewhere in the package configuration. Unfortunately, I can not find it myself, so I would be very grateful if someone could tell me what exactly it is.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daria Motorina, 2019-11-29
@glaphire

Autoload path error in composer.json

"autoload": {
        "psr-4": { 
            "Import\\": "src/" 
        }
    }

Here it is necessary to correctly register the paths, because now he sees them incorrectly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question