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.
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.
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/"
}
}
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/"
}
}
composer.json
"autoload": {
"psr-4": {
"Book\\": "src/Book/",
"Vehicle\\": "src/Vehicle/"
}
}
Here are the major differences,
1. For example if you define that the Acme\Foo\
namespace is anchored in src/
,
Acme\Foo\Bar
in src/Acme/Foo/Bar.php
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 workAcme\Foo\Bar
---> src/Acme/Foo/Bar2.php
(for Bar class) will not workEven 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. ;-)
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/