Autoloading plugin classes with Composer?

I published a plugin where its classes are loaded via Composer’s autoload JSON setting. It works perfectly, but the obvious downside is that users that don’t have Composer won’t have those classes loaded.

I know that Kirby ships with the load() helper whose job is to provide a middle ground. My question is - what’s the difference between using load() and the Composer autoload feature?

Oh, I didn’t know about the second link. Yet this didn’t answer my question. I know the two ways to set up plugin classes and dependencies. My question - is one superior?

I feel like the Composer setup is more hassle for no benefit:

  • you need to track the vendors folder in Git
  • you include a file that may not exist
  • you need to remember to run composer install before each deploy
  • you need to modify your .gitignore

With the load() function:

  • you still have autoloading
  • all installations have the same behavior (less prone to errors)

Judging by that, load() is clearly better. I’m asking if it worse at a lower level compared to the Composer way? Do they autoload classes differently? I can see that @distantnative uses load() in his Retour plugin.

Yes, @distantnative’s plugin uses load() because he only loads his own classes, not any external libraries packages that can or even must be installed via composer.

Yes. If you use external Composer packages, of course you would use the approach with tracking the vendor folder. I don’t think there’s any other sensible way to do it.

However, if you have your own classes only - is there any detriment in using load() over Composer’s autoloader? Do both approaches work the same at a lower level? My guess is that you should use load() when you have just a few classes (because it’s easier and quicker), and use the vendor folder when you have a lot of classes that would be hard to manage by hand in load().

No, as far as I understand the docs, that is the suggested procedure. Maybe unless your own classes are composer packages themselves.

Yes.

2 Likes