What Are the Differences Between PSR-0 and PSR-4?
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.
Difference between PSR-4 and classmap autoloading?
PSR-4 standard requires of you a strict filesystem structure based on namespaces. Say you have an app in src
directory with App
namespace, then all sub-namespaces will mirror subdirectories and class names will be the same as file names without the .php
extension.
{
"autoload": {
"psr-4": { "App\\": "src/" }
}
}
src/
Foo/
Bar.php <---- App\Foo\Bar class
Baz.php <---- App\Baz class
The autoloader then "knows" where to look for the class of a certain fully qualified name and therefore doesn't require the dump-autoload command to sniff files for classes.
Performance issues are then solved with composer dump-autoload --optimize-autoloader
flag, or -o
, which will generate class map a similar way the classmap autoloading does.
On the other hand, classmap autoloading does not require you to have almost any certain file or directory structure, it will recursively go through .php
and .inc
files in specified directories and files and sniff for classes in them.
{
"autoload": {
"classmap": ["src/", "lib/", "Something.php"]
}
}
Those classes are then added to a list (cached in a PHP file in vendor/composer directory) which is used for autoloading.
Any new class then must be added to that list by running composer dump-autoload
command.
Composer - Autoload and PSR-0 vs PSR-4
You didn't need 2 entries just one for the main namespace so something like this for PSR-4:
"autoload" : {
"psr-4" : {
"MyApp\\" : "/src" }
}
As long as everything in src/ uses the same namespace that's all you'll need. Just let the autoloader do it's job.
As to which to use I'd go with PSR-4 because at some point it is expected that PSR-0 will be deprecated and as PSR-4 is made to be backwards compatible minus some warts for older legacy programs there isn't really a difference except of you start using some of it newer features
Which version of PSR is valid?
You can see the PSRs as one huge coding standard. They depend on each other but every PSR stands alone for itself. PSR-2 describes Coding Style Guide while PSR-4 describes the standard for Autoloading.
Here is a website about the PHP Standards Recommendations. You can find there a full list of all existing PSR.
PSR means PHP Standard RECOMMENDATION - so it is only a recommendation on how to handle/format a specific part of a php-application. The index is just a ID to identify what the psr is good for. Do not understand it as a version-number. PSR-0 and PSR-4 both cover the handling of Autoloader.
Understanding PSR-4 Composer without any framework
The problem is your file isn't where you told autoloader to look. Your file is in /vendor/abkrim/rclub/src/ResellerApi.php
and you've told autoloader that the namespace Abkrim\Rclub
is in vendor/abkrim/rclub/
, so by specifying Abkrim\Rclub\ResellerApi
you're telling autoloader that ResellerApi is in /vendor/abkrim/rclub/ResellerApi.php
, see the problem? You missed out the src folder.
Try changing your composer.json file to:
"autoload": {
"psr-4": {
"Abkrim\\Rclub": "vendor/abkrim/rclub/src/"
}
}
Once you've made the change make sure to run composer dump-autoload
to update autoloader.
Alternatively you can update your ResellerApi.php
and reseller.php
files to use Abkrim\Rclub\src
.
Why use a PSR-0 or PSR-4 autoload in composer if classmap is actually faster?
Why use a PSR-0 or PSR-4 autoload in composer if classmap is actually faster?
Because it's more practical.
In production, you can use a classmap (with composer dumpautoload -o
) because you won't add any new class, but in dev environment it's interesting to have the flexibility provided by PSR-0 or PSR-4 (i.e. nothing to do when adding new classes).
Update: you can also use composer install -o
, it's simpler.
Related Topics
How to Handle Double Quotes in String Before Xpath Evaluation
How to Catch a "Catchable Fatal Error" on PHP Type Hinting
Laravel 4 - Logging SQL Queries
Why Does Tcpdf Ignore My Inline CSS
How to Check If $_Get Is Empty
Ajax Post Request in Laravel 5 Return Error 500 (Internal Server Error)
Can Someone Explain the /E Regex Modifier
Header Location Not Working in My PHP Code
How to Open a Link in Private Browsing Mode
Store Procedures in PHPmyadmin
PHP Simplexml Namespace Problem
How to Get CPU Usage and Ram Usage Without Exec
How to Auto Call Function in PHP for Every Other Function Call
Directory Index Forbidden by Options Directive