Composer autoload custom classes
Asked Answered
S

1

6

I try to autoload my custom pdo class with composer. Ran the following command to update autoload:

compser update
composer install

Both seem to work, no error prompted. But,

vendor/composer/autoload_namespaces.php

Does not list the custom namespace added to composer.js.

File structure

-Root
 ->classes
   ->pdo
     ->class.php
 ->vendor
   ->various extensions loaded with composer
index.php

PHP Class

namespace Classes\Pdo;

Class DB {
    //Do some stuff...
}

Composer.js

"autoload": {
    "psr-4": {
        "Classes\\Pdo\\": "classes/pdo"
    }
}

Index.php

$pdo = new \Classes\Pdo\DB(); //Fatal error: Class 'Classes\Pdo\DB' not found
Shroud answered 12/1, 2016 at 16:4 Comment(3)
Have you tried composer dump-autoload?Unusual
That just solved it, thanks!Shroud
No idea what composer dump-autoload does, but your question was the answer for me.Flattery
I
1

Old question, but I just ran across this myself.

For future Googlers, in my case the issue turned out to be the name of the class file did not exactly match the class name.

See this post: Why does 'composer dumpautoload -o' fix 'Class not found' PHP error?

Infrared answered 30/10, 2019 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.