Composer (see here) is a system to organize your PHP code. With this system, you can create or maintain PHP files by referring to snippets that are stored in different places. This is called “dependency management”.
Basic building blocs of PHP code can be stored in “libraries”. Libraries can be shared across projects. If they’re hosted on a server, you can access libraries that other people wrote. You might want to refer to libraries and code snippets that you’ve created for other projects.
Composer is practically both a database and a compiler:
- Database: it stores the names and locations of PHP code snippets. It’s like a directory or address book in this respect.
- Compiler: given proper instructions, creates files by concatenating referenced snippets.
Composer works from (relies on) a composer.json file.
The advantage of using such a system is that you don’t need to repeat any functions throughout your code or project. It’s enough to have coded it once, you can call it again to build/create different files that might need it.
For instance, if a multisite-website has 4 different websites but you want the same information to appear in the header of each, you can use composer to
- create the header.php file adapted to each website,
- place it in the right destination folder,
- all the while relying on code that has been written only once
- so you don’t have to do any dangerous copy-pasting.