Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Are the Differences Between PSR-0 and PSR-4?

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...

  • Battle of the Autoloaders
  • Laracasts PSR-4 autoloading
  • PSR-0
  • PSR-4

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.

like image 208
Varun Nath Avatar asked Jul 21 '14 15:07

Varun Nath


People also ask

What PSR-0?

PSR-0 looks at the namespace of a class and discerns its location on the hard drive from that bit of information. For example, the class \Zend\Mail\Message would lead to /path/to/project/lib/vendor/Zend/Mail/Message.

What PSR-4?

PSR-4. Autoloading Standard. It describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be auto loaded according to the specification.

What is PSR-4 autoloading standard laravel?

The PSR-4 autoloading standard requires the fully qualified class name to match the filesystem path, and is case-sensitive. The namespace prefix is mapped by the psr-4 option in your composer. json .


2 Answers

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.

like image 199
Seldaek Avatar answered Sep 24 '22 12:09

Seldaek


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
like image 37
Adil Abbasi Avatar answered Sep 22 '22 12:09

Adil Abbasi