Let's Talk About Composer

ยท

3 min read

Let's Talk About Composer

If you don't know, composer is a PHP dependency manager. It is the PHP version of NPM (Node Package Manager).

Dependencies simply means the codes your code uses. That is, if my post object uses a category object I or someone else wrote, the category object is the product class' dependency.

If you've been following PHP for some years, you'd know before PHP 7, PEAR was quite popular. PHP Extension and Application Repository (PEAR) had to take the back seat upon the introduction of Composer.

Composer was introduced in 2015 and it is the de facto way of managing and using dependencies.

One sweet thing about Composer is the namespace and autoload. Gone are the days of require, require_once, and their cousins include and include_once. You can literally write your code, give it a namespace you fancy and use the namespace anywhere in your code so far the folder is a part of the autoloaded folder. This is actually very simple in terms of code.

I'm not saying include and require are bad but when you include or require, you are basically importing the whole file into the one it is needed.

What if there are variables with similar names in both (could be more than two) file for example? This would lead to an error, a quite unnecessary error if you ask me.

With composer, the files are only required in one place, the index.php file. That's all, it actually works like magic because the code have been written by excellent PHP developers to make your life better.

Composer requires the knowledge of OOP because everything is objects and classes, this reminds me of Java. There some rule in PHP to follow when using composer or writing any PHP code whatsoever, PSR i.e. PHP Standard Recommendations. Check them out here

ย