265

Recently I've read about namespaces and how they are beneficial. I'm currently creating a project in Laravel and trying to move from class map autoloading to namespacing. However, I can't seem to grasp what the actual difference is between PSR-0 and PSR-4.

Some resources that I've read are...

What I understand:

  • PSR-4 does not convert underscores to directory separators
  • Certain specific rules of composer cause the directory structure to become complex which in turn makes PSR-0 namespacing verbose and thus PSR-4 was created

Examples explaining the difference would be appreciated.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Varun Nath
  • 5,570
  • 3
  • 23
  • 39

5 Answers5

316

They are very similar so it is not surprising that it's a bit confusing. The summary is that PSR-0 had some backwards compatibility features for PEAR-style classnames that PSR-4 dropped, as such it only supports namespaced code. On top of that PSR-4 does not force you to have the whole namespace as a directory structure, but only the part following the anchor point.

For example if you define that the Acme\Foo\ namespace is anchored in src/, with PSR-0 it means it will look for Acme\Foo\Bar in src/Acme/Foo/Bar.php while in PSR-4 it will look for it in src/Bar.php, allowing for shorter directory structures. On the other hand some prefer to have the full directory structure to clearly see what is in which namespace, so you can also say that Acme\Foo\ is in src/Acme/Foo with PSR-4 which will gives you the equivalent of the PSR-0 behavior described above.

Long story short for new projects and for most intents and purposes, you can use PSR-4 and forget all about PSR-0.

Alex_Nabu
  • 311
  • 3
  • 11
Seldaek
  • 40,986
  • 9
  • 97
  • 77
  • 19
    It picks `src/Bar.php` if you say `Acme\Foo\ => src/` – Seldaek Jul 21 '14 at 20:07
  • 4
    PSR-4 is slower than PSR-0, isn't it? – Nguyen Linh Aug 26 '15 at 09:09
  • 3
    @NguyenLinh I don't think so. It does the same thing, but possibly with fewer levels of directories, so it might actually be a bit faster. Measure it. You can create a package that you can switch between PSR-0 and PSR-4 - I don't think you will see a difference. – Sven Oct 05 '15 at 19:16
56

Here are the major differences,

1. For example if you define that the Acme\Foo\ namespace is anchored in src/,

  • with PSR-0 it means it will look for Acme\Foo\Bar in src/Acme/Foo/Bar.php
  • while in PSR-4 it will look for Acme\Foo\Bar in src/Bar.php(where Bar class is).

2. PSR-4 does not convert underscores to directory separators

3. You would prefer using PSR-4 with namespaces

4. PSR-0 will not work even if the class name is different from file name, like considering above example:

  • Acme\Foo\Bar ---> src/Acme/Foo/Bar.php (for Bar class) will work
  • Acme\Foo\Bar ---> src/Acme/Foo/Bar2.php (for Bar class) will not work
Adil Abbasi
  • 3,161
  • 1
  • 40
  • 35
42

PSR-4 is something like 'relative path', PSR-0, 'absolute path'.

e.g.

config:

'App\Controller' => 'dir/'

PSR-0 autoload:

App\Controller\IndexController --> dir/App/Controller/IndexController.php

PSR-4 autoload:

App\Controller\IndexController --> dir/IndexController.php

And there are some more difference in details between PSR-0 and PSR-4, see here: http://www.php-fig.org/psr/psr-4/

Kacifer
  • 1,334
  • 17
  • 26
33

Namespace/ folder convention.

Classes should be stored in folders according to their namespaces.

In general, you will create an src/ directory in your root folder, sitting at the same level as vendor/, and add your projects there. Below is an example of the folder structure:

.
+-- src
    |
    +-- Book 
    |   +-- History
    |   |   +-- UnitedStates.php - namespace Book\History;
    +-- Vehicle
    |   +-- Air
    |   |   +-- Wings
    |   |   |   +-- Airplane.php - namespace Vehicle\Air\Wings;
    |   +-- Road
    |   |   +-- Car.php - namespace Vehicle\Road;
+-- tests
    +-- test.php
+-- vendor

Difference between psr-0 and psr-4

psr-0

It is deprecated. Looking at vendor/composer/autoload_namespaces.php file you can see the namespaces and the directories that they are mapped to.

composer.json

"autoload": {
        "psr-0": {
            "Book\\": "src/",
            "Vehicle\\": "src/"
        }
} 
  • Looking for Book\History\UnitedStates in src/Book/History/UnitedStates.php
  • Looking for Vehicle\Air\Wings\Airplane in src/Vehicle/Air/Wings/Airplane.php

psr-4

Looking at vendor/composer/autoload_psr4.php file you can see the namespaces and the directories that they are mapped to.

composer.json

"autoload": {
    "psr-4": {
        "Book\\": "src/",
        "Vehicle\\": "src/"
    }
}   
  • Looking for Book\History\UnitedStates in src/History/UnitedStates.php
  • Looking for Vehicle\Air\Wings\Airplane in src/Air/Wings/Airplane.php

composer.json

"autoload": {
    "psr-4": {
        "Book\\": "src/Book/",
        "Vehicle\\": "src/Vehicle/"
    }
}    
  • Looking for Book\History\UnitedStates src/Book/History/UnitedStates.php
  • Looking for Vehicle\Air\Wings\Airplane in src/Vehicle/Air/Wings/Airplane.php
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
-7

Even when I tried but Composer is a mess. Sadly, it's the only alternative.of the market.
Why is a mess?.
The Composer's autocomplete works fine if you are in control of the code. However, if you are importing a different project, you find yourself with lots of styles and ways to create folders. For example, some projects are /company/src/class.php while others are company/class.php and others are company/src/class/class.php

I created a library that solves it:

https://github.com/EFTEC/AutoLoadOne (it's free, MIT).

It generates an autoinclude by scanning all the classes of a folder, so it works in every case (psr-0 psr-4, classes without namespace, file with multiple classes..

edit: And again, downvoted without any reason. ;-)

magallanes
  • 6,583
  • 4
  • 54
  • 55
  • 1
    Read up about the classmap option in composer.json. https://getcomposer.org/doc/04-schema.md#classmap - might be the reason for downvoting your answer. – Patrick Feb 10 '19 at 03:50
  • @Patrick I looked at his library. It's an interesting approach. I mean, for me it's too much just to avoid composer's classmaps, but it's ok. – Lawrence Mar 24 '22 at 12:39