4

Brand new to composer. Have it installed and running and have since installed two packages to use on my site by requiring the autoload at the top of the page :

require $_SERVER['DOCUMENT_ROOT'].'/../vendor/autoload.php';

then my use whatever\whatever;

My question is... is there a way I can load single packages rather than everything with the autoload? In this case I am using two packages on the site, but they will never be used together... so... I would think it would make more sense to only load the one(s) I need to use on each page right? There would be a performance difference loading everything with autoload compared to only what I need right?

I've looked around, but can't seem to find an answer to this and if it is possible - maybe I am searching with the wrong terms or looking in the wrong places.

user756659
  • 3,372
  • 13
  • 55
  • 110

1 Answers1

7

You are already only loading the classes you really need. There is no performance benefit not doing this without the autoloader, but a huge drawback: You will be forced to add every needed class manually if you don't use the autoloader.

You will have a case here if you can prove by measurement that one approach is significantly better than the other.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • Let's say I add 10 packages, but I still only use one of those 10 by itself on a single page. Still use the autoloader? I don't see how loading 9 packages I know I won't use could be a benefit. – user756659 May 17 '16 at 18:38
  • You don't load 10 packages, you'll load 10 array entries for the autoloader, i.e. a php array in a file containing 10 keys as the namespace prefix and 10 values with the path to the package. That's all. Only when you actually execute code that needs one of the classes, more code is loaded. – Sven May 17 '16 at 18:54
  • Okay, that makes much more sense then - the autoloader is basically a location list that points to the files needed ONLY when `use whatever\whatever;` is used. That clears everything up for me. – user756659 May 17 '16 at 19:27
  • 1
    Note that `use whatever\whatever` does not autoload anything. It just writes a link into the internal namespace list for that file. The autoloading usually takes place only when using code that got imported that way, for example `new whatever()`. – Sven May 17 '16 at 22:55