Search code examples
packagist

Autoloading Php Packages with Hyphens


Packagist does not allow package names to have capital letters. To workaround this, it recommends using hyphens -. Thus my package name went from TableCreator to table-creator. Unfortunately, this seems to have prevented my library from autoloading with the following error message:

Class 'Company\TableCreator\DatabaseField' not found

This error message disappears as soon as I manually include the specific file rather than relying on the vendor/autoload.php file.

My packages composer.json file is as follows

{
    "name": "company/table-creator",
    "type": "library",
    "description": "Package creating or editing MySQL tables.",
    "keywords": ["mysql", "mysqli","models"],
    "license": "MIT",
    "authors": [
        {
            "name": "xxx xxx",
            "email": "[email protected]",
            "role": "Developer"
        }
    ],
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "psr-4": {
            "company\\table-creator\\": ""
        }
    }
}

The namespace declared in the file is still namespace Company\TableCreator;

What do I need to tweak in the composer config for the classes to autload now that the package name has a hyphen?


Solution

  • You need to revert the change to the PSR-4 namespace prefix:

    {
        "autoload": {
            "psr-4": {
                "Company\\TableCreator\\": ""
            }
        }
    }