Autoload & PSR in PHP

autoload function in php

Developer 101: As we build pages in PHP, we use lots of files (written earlier in the development) and we call on them as needed on each pages. So instead of writing all the file names, we can use Autoloader feature. Autoloader reduces stress to include all the files again and again in each and every page. It is used to automatically load class in your project. In composer JSON file you can check “autoload” section where we can put all class files which can be used in project.

What is PSR?

PSR stand for PHP Standard Recommendation. PSR have its own index status. We are using PSR for naming conversion and coding style guidelines.

Here we will go through about most useable PSR index for projects.

PSR index Steps

1) PSR-0:- It is used for huge forward step for reusable code (same code for multiple purpose). If your project developed on PSR base components and coding rules, then you can use PSR-0 easily. You can put your class files inside the vendor folder in your project directory to create composer file and put your class autoload PSR-0 section. For this you must use composer in your project. You can check how to make your own class in autoload composer.

2) PSR-1:- It is used to focus on basic coding standard. In this case PSR-1 checks your php tags Like

Before using PSR-1, you should follow some basic rules of PSR-1

PSR-1 is using UTF-8 without BOM for PHP code, Enforce PSR-0, Class name must be declared Studlycaps, Class constants must be declared in uppercase with underscore separator, Method name must be declaring camelCase.

3) PSR-2: It is used for the single coding structure which result should be uniformly formatted. PSR-2 extends to PSR-1 basic coding standard. In an ideal world every project should adopt the PSR-1 and PSR-2 methods.

4) PSR-3: It used for logging interface. It is use for system log protocol like: Debug, Notice, Info, Warning, Critical and Emergency alert.

5) PSR-4: It used for huge directory files autoload for you. PSR-4 cover major points like

1) Namespacing your classes.
2) Telling composer where you find your classes
3) Generating composer autoloader
4) Using composer, you can put all files access by autoloader

We can use composer Like

"autoload": {
		"psr-4": {
			"Yourfoldername\\currentfolder\\": "foldername where your classes available"
For Example I have folder structure Like:


It will work in json autoload Like:

	"autoload": {
		"psr-4": {
			"myfolder\\classfolder\\": "classfolder/"

In the class folder, we can tell composer to look in class folder directory to access class folder classes. When you use the class in autoloader make sure your namespace must be correct naming.

About Author:

Ravi Patel is a full stack web developer, working with PHP, NodeJS, AngularJS. He has been an integral part of cmsMinds team. Self-learner, strong command on logic implementation, best practice with many frameworks, are some of the personality traits of Mr. Ravi Patel.

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Quick Inquiry