4
votes

The project I'm working on requires using the PHP SDK's from multiple 3rd parties. Two of these are Amazon Web Services and the Google API Client (for Google+), and both of them use Composer to manage their files / dependencies. I'm not sure how to best set it up code / structure wise, though, because I don't need both AWS and Google loaded together. I might need AWS in one area and Google in another, so I don't want to just autoload everything every time and have the additional overhead from libraries I don't need right then. Right now I have the structure set up like this:

  • awscode.php
  • googlecode.php
  • libs
    • composer.json
    • composer.lock
    • vendor
      • autoload.php
      • aws
      • google

So, everything Composer related is in a shared composer.json file, and all vendor files in the single vendor directory. But, I can't seem to find a way to just load up say AWS. It wants me to use the autoload.php from what I can tell, which seems to want to load up everything.

Do I need to set it up more like this if I want control over each library?

  • awscode.php
  • googlecode.php
  • libs
    • aws
      • composer.json
      • composer.lock
      • vendor
        • autoload.php
        • aws
    • google
      • composer.json
      • composer.lock
      • vendor
        • autoload.php
        • google

I'm obviously new to Composer and how to best utilize it, and want to make sure that I am setting it up the best way for both my situation, and for future management.

2
Are you sure you still cannot get packages from different vendor directories? I just included autoload.php from both directories an it works fine from me. - Kirby

2 Answers

4
votes

When using Composer, it only loads the classes when they are actually called in your code. To my knowledge this uses the PHP spl_autoload_register.

So in answer to your question, there won't be a significant extra overhead (if any).

2
votes

Autoloading means that the file which defines a class gets read when you first use that class.

You should include all your project dependencies in one composer.json, they won't be loaded in files you don't use them in.