0

When installing libraries with composer it generates a autoload_classmap.php in order to hook up the different namespaces with classes. For example:

'PHPUnit_Framework_Assert' => $vendorDir . '/phpunit/phpunit/src/Framework/Assert.php'

Why is this even needed when it can be done with a simple spl_autoload_register() and bypass the whole need for generating new files every time?

Mujtaba Haider
  • 1,650
  • 2
  • 19
  • 29
user2906759
  • 821
  • 1
  • 9
  • 15

2 Answers2

2

Because generating a list of all classes and where they can be found is an expensive task which you really don't want to have to do every single time the autoloader is invoked, or even just every time for a new request. You just need to compile this information once every time your vendor folder changes and store it somewhere. And that's exactly what Composer does.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

Composer uses a PSR-0/4 autoloader with some features to register non compliant with PSR-0/4 classes and autoloaders.

If you use the option --optimize-autoloader (-o), It will generate a class map to get a faster autoloader.

References

  • Note that classmap is not faster in every case! http://stackoverflow.com/questions/22803419/why-use-a-psr-0-or-psr-4-autoload-in-composer-if-classmap-is-actually-faster/22823995#22823995 – Sven Sep 08 '14 at 19:03
  • but this is the reason. http://framework.zend.com/manual/2.0/en/modules/zend.loader.class-map-autoloader.html. – Édipo Costa Rebouças Sep 08 '14 at 20:22
  • With small classmaps, the overhead I described in my answer is so small, any file operation will use as much time. With a classmap having thousands of classes, loading that map on every script run is considerable overhead if not all of these classes are used. Measure which variant is actually faster in your application: Dump both the optimized and regular autoloader and run a performance test. – Sven Sep 09 '14 at 01:58
  • I agree with your point of view, the class map probably is not faster in every case, like the search algorithms, quick sort, merge sort, bubble sort... Each one in some cases is more faster then another. The point is that the reason for this approach and not the case that class map is faster or slow. – Édipo Costa Rebouças Sep 09 '14 at 14:06
  • 1
    You give a link, stating "this" (the link) is the reason, without describing which reason you mean. The link points to an old version of Zend Framework 2 describing autoloading - the usefulness of framework powered autoloading is diminishing with the usage of Composer. Composer offers native classmap loading only to allow old class structures in old, but useful software packages, to be autoloaded. The preferred way to do it is to use PSR-4 for namespaced classes, and PSR-0 in all other cases. – Sven Sep 10 '14 at 20:41
  • http://getcomposer.org/doc/03-cli.md#install see the option -o. We both are not completely right about our answers. – Édipo Costa Rebouças Sep 11 '14 at 18:57
  • I know the `-o` option dumps all classes as classmap. My point however remains valid: Pushing ALL classes into a classmap for autoloading is not the most efficient way in every case. By using the classmap for a project, the developer forces that potential performance issue onto his users. Using PSR-0/4 gives the choice of using a classmap back to the user of a package. And it is easier during development: You don't have to constantly dump the autoloader after adding a new class. – Sven Sep 11 '14 at 22:17
  • How I say before, I agree that class map is not the most efficient way in every case. Probably the most efficient way is use require_once or custom spl autoloader with your fine tuning. The question of Mark Bower is why composer use the classmap, the response is, perfomance. You can talk with guys of composer project to make a better optimization, but this is not the question of Mark Bower. – Édipo Costa Rebouças Sep 11 '14 at 22:32
  • I have to disagree again: `require_once` has the additional overhead of doing the bookkeeping for which files were already required. This is less efficient than using `require` - which you can do for autoloading, because every unknown class is only autoloaded once. And a custom and ultra performant autoloading function would be hard to write, because the Composer autoloader is said to be very close to possible maximum speed already. All you could do is to fix less than optimal autoloading definitions in a package you were using. – Sven Sep 11 '14 at 22:37
  • How I say before, This is not the question of Mark. If want speak more about why use standard autoloader or class map, autoloader optimization, make this in a private chat. – Édipo Costa Rebouças Sep 11 '14 at 22:46
  • Mark didn't even ask a question. – Sven Sep 12 '14 at 00:01
  • Sorry, the user something, if this is important. – Édipo Costa Rebouças Sep 12 '14 at 00:04