I am creating a PHP project and want to implement PSR-4 autoloading.
I don't know which files I need to create in the vendor directory to implement autoloading for class files.
I am creating a PHP project and want to implement PSR-4 autoloading.
I don't know which files I need to create in the vendor directory to implement autoloading for class files.
Copyright © 2021 Jogjafile Inc.
If you are using
composer, you do not create the autoloader but letcomposerdo its job and create it for you.The only thing you need to do is create the appropriate configuration on
composer.jsonand executecomposer dump-autoload.E.g.:
By doing the above, if you have a file structure like this
After executing
composer dump-autoloadthe autoloader will be generated onvendor/autoload.php.All your classes should be nested inside the
Appnamespace, and you should put only one class per file.E.g.:
And you need only to include the autoloader in your entry-point script (e.g.
index.php).Which will allow you to simply load your classes directly from anywhere after this point, like this:
This is explained at the docs, here.