Let’s talk about composer

What you’ll need


Alright. This one might be for the really new to composer and feel that their PHP is going somewhere.

It’s probably true that you’ve worked with javascript modules and its package manager NPM before. This is like that, but for PHP. Well, it’s a start of it. We’ll get to that later.

Gif time!


So let’s say you’ve got a basic php project up and running and you want to add some new functionality, but var_dump is a pain when working with large ammounts of data. So you want to use Symfony’s dump-component, var-dumper.

So let’s get started! First we need to install composer. You’ll find it at Composers website. Begin by following their four step guide on the /download page.

Once that is done you should be able to run composer -v in your terminal and get some info in return. Sweet, step one done!

Start a new project

Start by running composer init in your repository. This will lead you to a series of small questions like author name, description and ability to search for libraries. If you’re like me you’ll probably just skip all of these by hitting enter a bunch of times.

Once this is done a composer.json file should now exist in your project folder. The composer.json file can contain a lot of different kind of data like info about the project, dependencies, dev-dependencies, pre and post commands and autoloading. In this case it will only serve for dependencies. But for now we’re done with this step.

Downloading a package

Okay, so we’ve downloaded composer, created a composer project and now ready to download a package. Like I mentioned before, we’re going with the var-dumper. So simply run (still in the same directory) composer install symfony/var-dumper and wait for the terminal to do its thing!

Hopefully your terminal that all went well and you’ve installed var-dumper at some version. Awesome!

Start using your new packages.

So, the things is here. We cannot use our new package right away. You see, when you installed the package a /vendor folder was created and in it a symfony folder and in that folder (!!) there’s your var-dumper package. This does not mean that the package is available through the global scope. We have to include the vendor folder and its packages into our own application. That means that you have to do a require /path/to/vendor/autoload.php (with a actual suitable path) in your index file equivalent to your setup.

If you’ve tagged along and understood what I’m trying to say you’r process should look like this.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

cd to/your/project

composer init


composer install symfony/var-dumper

And your index.php should look like:


require __DIR__ . '/vendor/autoload.php';

You should now be able to do stuff like:


require __DIR__ . '/vendor/autoload.php';

$data = ['one', 'two'];


Working? Success!


So fast forward this 1-2 weeks and you’ve built up your new addiction and are now using 57 different packages. Some of these packages might need an update. This is an easy fix!

composer update

Or if you only want to update a specific package

composer update author/package

If you want to use composer at its most basic level - there it is! Hopefully you’ve managed to follow along and learnt something!