Reputation: 3155
I was just creating one of my first PHP Packages using Composer and I hit a problem.
Currently I'm working on a package that his main class extends a class from another package, so on my composer.json
I've the package I'm extending declared as a requirement:
"require": {
"php": ">=5.3.0",
"ext-mbstring": "*",
"author/BasePackage": "dev-master"
},
And, If I issue composer update
I get a nice vendor
directory inside my project with the author/BasePackage
dependency downloaded there.
So my question is, on my main class file src\Extended.php
I need to manually do something like require_once __DIR__ . "/../vendor/autoload.php";
in order to autoload the BasePackage
so I can do class Extended extends \BasePackage\BasePackage
.
Is this ok? How am I supposed to load my package dependencies? When I commit this package to GitHub or so, I'm ignoring the vendor
directory and if someone uses this by requiring it on their projects require_once __DIR__ . "/../vendor/autoload.php";
will fail because it won't be where I expected it to be.
How should I do this?
Thank you.
Upvotes: 0
Views: 1249
Reputation: 70863
No, this is not ok.
You can safely assume that anyone who is using your package is using Composer. And he will be using Composer's autoloader. So he is able to load both your own class from the package, as well as any other class your package depends on, provided that you listed it as a dependency in your package.
Don't manually load other classes! You don't have to.
Your tests in your package should include the vendor/autoload.php
in the bootstrapping - this will allow your class to directly be usable, because the other packages classes can be autoloaded as well. Any example scripts should also include the created autoloader.
If your package is used somewhere else, that autoloader will act the same: All classes that Composer manages to download that have a correct autoload declaration are directly usable after the autoloader has been included.
Upvotes: 4