PHP Composer autoloader class not found Exception

The name speaks for itself. So here is my project structure:

|src |Database |Core |MySQL.php |Support start.php |vendor composer.json index.php 

MySQL.php file:

 <?php namespace Database\Core; //Some methods here 

index.php and start.php files:

 //start.php file <?php require __DIR__ . '/../vendor/autoload.php'; ?> //index.php file <?php use Database\Core; require __DIR__ . '/src/start.php'; $mysql = new MySQL(); // Gets exception Class 'MySQL' cannot found etc. ?> 

And finally, my startup part of composer.json:

 "autoload": { "psr-4": "Database\\": "src/" // Also tried "src/Database" too } 

Where is the problem? I am really tired of trying to deal with this situation. Please help the guys! Thanks.

+7
php composer-php autoloader
source share
2 answers

You need to include the namespace when initializing the class:

 $mysql = new Database\Core\MySQL(); 

or

 use Database\Core\MySQL; $mysql = new MySQL(); 

See Using Namespaces: Merge / Import

+17
source share

Besides using the correct use statement, as already mentioned, PSR-4 does not work like that. It is rather a pseudonym. You say src is equal to Database . Thus, to have a directory called Database , it would imply that the full namespace + class is equal to 'Database \ Database \ Core \ MySQL`. Do you want to use PSR-0 in this case or edit the definition of PSR-4.

+4
source share

All Articles