PSR-0 Autoloading With Composer - Tutorial With Practical
PSR Stands for PHP Standard Recommendation. It actually allows us to take advantage of the experience of programming and concepts related to it, we have learned so far throughout the history of computer and programming. It provides us best practices and guidelines related to structure, auto-loaders, coding style, caching, HTTP Message, Security, Container Interfaces , Events and many other concepts.
There are many PSR standards but here we are gonna talk about PSR-0 which is related to auto-loading.
What is PSR-0?
The PSR-0 standard is related to Auto-Loading standards. It describes the mandatory requirements that must be adhered to for auto-loader interoperability.
Before using PSR-0, here are the key points you should take into consideration:
- This standard is Deprecated and substituted by PSR-4 Standard. And, PSR-4 is also recommended by Composer.
- Using PSR-0 is not recommended for Full Applications, but you can use it for packages and other stuff like that.
- A fully-qualified namespace and class must have the following structure \<Vendor Name>\(<Namespace>\)*<Class Name>
- Each namespace must have a top-level namespace (“Vendor Name”).
- Each namespace can have as many sub-namespaces as it wishes.
- Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.
- Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The _ character has no special meaning in the namespace.
- The fully-qualified namespace and class are suffixed with .php when loading from the file system.
- Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case.
You can find some more info on PHP-Fig.
Example With Composer:
We are gonna make a simple example project using composer here for auto-loading.
- So, first create an index.php file in your project's root directly.
- Now, make a src dirctory also in the same directory (root directory of project). It will be the directory where you will put all of your packages (I will show the whole directory structure at the end, so you can take a look if you are confused).
- According to the first PSR-0 standard mentioned above, we have to put every package in the structure VendorName/Project/Namespace. So, we will make a directory/folder in src named TricksFlood. Now, inside that TricksFlood directory make a new directory Library. The folder structure will be src/TricksFlood/Library. So, now we are pretending that its a library/package made by TricksFlood.
- Now, we will put our namespaces inside the src/TricksFlood/Library directory. Create a PHP file Tricks.php in that folder. Inside the Tricks.php , put the following code:
We made a namespace according to the file's structure TricksFlood\Library. Then we
made a class Tricks. Make sure the name of class and the file are same and the first
letter of both is uppercase.
- Now come to the root directory, and create a composer.json file. Here we will tell composer that we wanna use PSR-0 standard. We have to mention our vendor name and the src dirctory (you can use your own directory to put all the packages). Put the following code inside that composer.json.
As you can see, we tell the composer that we wanna use auto-loading. And inside
auto-loading, we wanna use PSR-0 standard. Now tell the vendor name and the package's
root directory to composer in the format
"Namespace-Vendor-Name":"Packages-Root-Directory"
We are telling composer that there is a package whose vendor name is 'TricksFlood' and
that is placed inside src directory. Note that we have to mention those double back slashes escape character to tell composer that convert it to single backslash because the
json format will not accept single backslash. You can put as many packages as you want
then just mention them in the PSR-0 object.
- Now, let's move to our index.php that is placed inside the project's root directory. We have to require composer's autloader in our index.php file. Then we will just make a new object of the class Tricks that is in the package we wanna use (TricksFlood). So, here is what we will do:
As you can see, we just need to mention the Namespace\ClassName and done. Now if you
don't wanna use the full path every time you use this class, then you can use the 'use'
with your namespace and give it an any alias of your choice as follows:
- Still we need to one last step before we test it for the first and last time. We have to generate composer auto-loader classes and configurations. Make sure you have install composer globally, otherwise it is not gonna work. Open a terminal in the project's root directory and type the command composer dump-autoload
- Now, just you can test it with hosting it on local server or by using in terminal. I am gonna use it in terminal. So, here is the input of output:
PSR-0 Autoloading With Composer - Tutorial With Practical
Reviewed by Tayyab Mughal
on
September 01, 2020
Rating:
Reviewed by Tayyab Mughal
on
September 01, 2020
Rating:









No comments:
Thanks For Your Comment!