Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is php composer and what does dependency manager mean?

I'm trying to understand what exactly composer is and does. I'm totally new to it and what the concepts are that surround it.

The things I have problems with are:

  • What is a dependency? What does it mean in PHP?
  • What does it mean that composer is a dependency manager?
  • What is the main argument for using composer or a dependency manager in general?
like image 778
Mohammad Ghazi Istanboli Avatar asked Nov 05 '16 17:11

Mohammad Ghazi Istanboli


1 Answers

can you please explain the main reason why I should use composer and what is a dependency

You use it to install libraries made by other people.

Example: you're dealing with dates in your PHP application. You can use date function and DateTime class that comes with PHP. However, you need to display a human-readable date in format of "5 minutes ago" or "in 2 hours" etc.

So you get to developing and you realize that you're going to spend time on this feature. It's a nice to have, but it takes time to do it.

A wise developer will think "Someone definitely had the same issue as I did, let's see how they solved it" and you stumble upon this library: Carbon

Now you want to use this library because it takes care of your problem. Your option is to download it from github and add to your project manually, which means placing it in some directory, including it in your app etc.

OR you can use Composer and you can tell Composer that you want a specific version of the library. In your terminal, you type:

composer require "nesbot/carbon: ~1.21"

Composer downloads the specific version of the library, places it in vendor/ directory and provides you with an autoloader.

For you, that means you can:

  • Install libraries made by other developers
  • Track which version of libraries you installed and lock down your project to that specific library version
  • You get an autoloader, so it's simple for you to add libraries to your project, you don't have to manually type require or include because all you need to do is include vendor/autoload.php to gain access to all libraries installed via composer.

Since you most likely had issues with dates, databases, mailing and so on - other people had them as well and some of those people were awesome enough to create free code for the rest of us to use. Composer helps you get that code and manage it.

like image 131
N.B. Avatar answered Nov 14 '22 21:11

N.B.