What is dependency manager?
Let’s say in your application you need to send a tweet from your Twitter account, what would you do? There are a many PHP Twitter client out there.
The quick way is just download one of them and put all the files in your application root. Well not a smart choice! You are adding someone code to your code base you need to maintain it, update it and so forth. What if there is a way that you say I want Library XYZ in my project version 2.0 and everything would be ready magically!
Say welcome to Composer.
What is composer?
Composer is a tool that helps you to manage your PHP project dependency and setup and install PHP library that other developers shared in https://packagist.org/
How to use it
And let’s say I pick codebird-php to talk to Twitter API. I’d go to the library’s page in packagist and check the available version. Right now version 2.6.0 is the latest. I create a file named composer.json in my root of project:
1 2 3 4 5
1 2 3 4 5 6 7 8 9 10 11
And that’s it! If you look at the directory that composer.json is saved, you’ll find out that a new file composer.lock and a new directory vendor are created after you ran the above command.
To use the new library you need to include vendor/autoload.php to your PHP files:
1 2 3 4 5
As you can see you don’t need to worry about place that the library is downloaded all you need to do is just include vendor/autoload.php and use the libraries in your application.
Should I use it?
Definitely! Tools for managing dependencies are de facto in writing and building modern applications.
Composer is a powerful tool and it’s easy to mix up the idea and miss use. In the next post I’ll talk about the best practices in using composer.