PSR-0 and PSR-4 Autoloading

Spread the love

Open Mobile Version.

I had read what’s PSR-0 and PSR-4 multiple times and every time after reading it, I had said to myself “I know this now(finally)”. But eventually I didn’t.

Realized this while I was going through Laravel documentation(once again).
I read the docs on php-fig one more time and peeped inside the vendor directory, read a few composer.json files to understand it.
So this time writing it here so that I remember what exactly they are.


PSR-0 path, says where to start. From there, it matches the complete Namespace string.

Example :

"autoload": {
    "psr-0": {"JakubOnderka\\PhpConsoleColor": "src/"}

This will tell autoloader to start at __DIRNAME__/src ( __DIRNAME__ where composer.json is located), from there it will match each of the Namespace part to a directory.
So if you use JakubOnderka\PhpConsoleColor\XYZ in your code then following file will be loaded __DIRNAME__/src/JakubOnderka\PhpConsoleColor\XYZ.php


PSR-4 says – Tell me the Namespace prefix and its location, every part after this Namespace prefix, it will map to directories, within the directory.

"psr-4": {
    "Illuminate\\": "src/Illuminate/"


It says Illuminate prefix starts at __DIRNAME__/src/Illuminate ( __DIRNAME__ where composer.json is located), from here match each sub-namespace after the Namespace prefix to a directory. So something like Illuminate\Http\Request will map to __DIRNAME__/src/Illuminate/Http/Request.php

There is another difference that in PSR-0 each _ in class name referred to a directory(Zend Framework 1 made heavy use of it).
I hope that I will remember it now.

Posted in PHP

Leave a Reply

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