Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between PSR-4 and classmap autoloading?

In regards to Laravel, I got a question about Composer autoloading i.e. the difference between "psr-4" and "classmap" autoloading.

1 difference that I know is PSR-4 does not need repeated dumpautoload for every changes or addition of new files whereas classmap needs dumpautoload for every change in existing files containing classes or addition of new file in specified directory.

like image 488
RoMo Avatar asked Jul 31 '16 16:07

RoMo


People also ask

What's the purpose of PSR 4 autoloading?

Overview. This PSR 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 autoloaded according to the specification.

What is Classmap in composer JSON?

Classmap# The classmap references are all combined, during install/update, into a single key => value array which may be found in the generated file vendor/composer/autoload_classmap. php . This map is built by scanning for classes in all .

What is the difference between PSR-0 and PSR 4?

PSR -0 and PSR-4 are standards for autoloading classes. PSR-0 has backward compatibility but PSR-4 only support namespaced code. The folder structure is different for PSR-0 and PSR-4.

What is Composer's autoloader?

Autoloading: The classmap Directive You just need to provide a list of directories, and Composer will scan all the files in those directories. For each file, Composer will make a list of classes that are contained in that file, and whenever one of those classes is needed, Composer will autoload the corresponding file.


1 Answers

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.

like image 139
Finwe Avatar answered Sep 22 '22 17:09

Finwe