Create Autoloader using Composer

  • Create a new file composer.json in the main folder and insert the just curly brackets

  • Run, to generate the autoload structure :

Acme is a example of Company/Project name, and src is the directory that contains all the files we want include in our project.

  • Run, to generate the psr-4 autoload file, the command:

The content of the file will be the following one:

  • Require the file autoload.php in your main project file (e.g.: index.php):

  1. Use namespaces Acme in your project for the file not in subfolder but just in src: So for example if you have the class Test.php in src you just need to use this.

You don’t need to use src in your namespace because the autoload has the $basedir on src.

The general rule is >>> what is inside the namespace + Class Name

So for example if you have the folder User in src for the class Person:

You must have the following code structure:

Class Person.php in src/User/:

index.php:

huckbit

Programmer by day and mostly by night as well.