r/PHPhelp • u/sgorneau • Dec 12 '24
First Composer project, stuck
I'm a Drupal developer, so I'm familar with Composer itself. But, I'm starting a project of my own using Composer ... and I'm a bit stuck.
I have my composer.json, required the bundles I need, and put require 'vendor/autoload.php'; at the top of my php script. Errors on each method called unless I specifically import each class
e.g. use GuzzleHttp\Client;
That's gonna get old ... am I wrong in thinking that autoload should be handling this?
0
Upvotes
5
u/TolstoyDotCom Dec 12 '24
Autoload just loads the given class, you still have to indicate which 'Client' you're referring to with a 'use' statement.
4
u/thewallacio Dec 12 '24
Which IDE are you using? Most will generate the correct
usestatement for your namespace (which I'd encourage you to create your own of for your project).requireyour autoload.php in some kind of top level bootstrap process, so that every script that's called/requested loads it. In lower level classes, you're then able to reference your autoload dependencies by their namespace.So for random example class in your project, where you have a root namespace for your project called 'Sgomeau':
That's a gross simplification but essentially, the autoloader is just a map to where your code can find the dependencies, you still have to reference them explicitly.