简体   繁体   中英

Custom package's class won't auto-load with composer autoloader

I'm trying to publish a new package on Packagist.org but I'm having some troubles using the autoloader system.

While I'm installing my package ( https://packagist.org/packages/gabyfle/gsteam-auth ) everything is okay, but when I'm loading it throught PHP:

require __DIR__ . "/vendor/autoload.php";

$testing = new \Class\SteamAuth('test', 'test');

I got an error, telling me that Class namespace cannot be find.

Fatal error: Uncaught Error: Class 'Class\SteamAuth' not found in <path>\gSteam-test\testing.php:4 Stack trace: #0 {main} thrown in <path>\gSteam-test\testing.php on line 4

I already tried every different autoload conventions (PSR0 and PSR4), I also tried to include my class in the autoloader throught the classmap parameter, but my class isn't loading.

Do you have any idea on how I can make my class being loaded throught the composer's autoloader?

Thanks

I would suggest you to use PSR-4 autoloading. Keep in mind that you have to put trailing backslashes at the end of the namespace declaration:

"autoload": {
    "psr-4": {
        "Gabyfle\\": "src/"
    }
},

Quote from the composer docs:

Note that as opposed to the older PSR-0 style, the prefix (Foo\\) is not present in the file path.

And also:

Namespace prefixes must end in \ to avoid conflicts between similar prefixes. For example Foo would match classes in the FooBar namespace so the trailing backslashes solve the problem: Foo\ and FooBar\ are distinct.

So keep that in mind if you switch from psr-0 to psr-4

Thanks to @mdexp, I found what's wrong with my class. I was defining two classes in one file, and that's not matching with the PSR-4 norme.

I just deleted one class from the file, and everything is now working fine.

Thanks !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM