DEV Community

Arif Hosen
Arif Hosen

Posted on

Auto Load in PHP (Shortly Understanding)

Image description
This article is written for learning purpose. So please if I made any mistake, please let me know and any feedback will be highly appreciated.

When we learn about autoloading, Firstly, we need to understand PSR.

PSR: PHP Standard Recommendation — is a set of standards that ensure working with PHP is easier. Example: PSR1- Coding standard, PSR7- HTTP request standard, PSR4- Autoloading standard.

PSR4: Provides the ability to include classes based on their file path. Like, you define BMW, and Mercedes classes in a different file and their namespace is Car. To load those files we need to include 2 files twice. Using PSR4 we can include files with autoloading. So, we don’t need include files of classes again again.

auto load — Sometimes we need multiple files of classes in a file, here we can use autoload.

Steps of using auto load-

Step 1: If you have installed composer. Run ‘composer init’ to the top directory of your project folder.
Step 2: Add the “autoload” property in the “composer.json” file, and add the “psr-4” property — its property is namespaces of classes and the value is file directory (where the auto load will find the namespaces).
> Example:
“autoload”: {
“psr-4”: {
“Car\\”: “oop/autoload/car/”
}
}

Step 3: Run “composer dump-autoload”
Step 4: Include the “vendor/autoload.php” file where you need those classes.

Note: When you include “vendor/autoload.php”, be careful about your current directory. For — “vendor/autoload.php” is in 3 steps upper directory from your current directory, here you use- (include_once “../../../vendor/autoload.php”).

Git repo: https://github.com/Arif-Hosen/learn_php/tree/main/oop/autoload/car

Top comments (0)