PHP: Creating Your First Composer Package

March 13, 2019
In this tutorial we'll be creating a Composer package hosted on GitHub.

Why Composer?

You should always split up your code when you can. You woulnd't wan't to write the same classes and functions for every project you're doing. You could just use submodules in git, but trust me, Composer is way easier, and has more features like autoloading, dependencies, etc.

Also, it's the standard package manager in the PHP community, used by Laravel, Symfony, Drupal and many more.

Installing Composer

Download the latest version via https://getcomposer.org/download/

Setting Up

Open your terminal, create a new directory called 'MyData' and cd to it.

Create your composer.json file by typing:

composer init
  1. Package name (vendor/name) - Typically you would use your GitHub username or organization as vendor. So for this example write [username]/mydata (change [username] with your github username)
  2. Description - Enter a short description of your package.
  3. Author - Defaults to your git config user.name and git config user.email.
  4. Package Type - Enter library
  5. License - For now just leave it empty, but later check out choosealicense.com
  6. Press Enter for the rest.

Writing the Code

In this example we are creating a class called MyData, which just stores data in an array.

Create a src directory, then create a new file: src/MyData.php

<?php
namespace [username]\MyData;

class MyData {

	private $data;

	public function set(string $key, $value) {
		$this->data[$key] = $value;
	}

	public function get(string $key) {
		return $this->data[$key];
	}
}

Again, change [username] with your github username.

Autoload Files

Now we have to make sure Composer autoloads all files in the src dir. So it will be included via require 'vendor/autoload.php';

Edit your composer.json file, and add this:

	"autoload": {
		"psr-4": {
			"xy2z\\MyData\\": "src/"
		}
	}

Also, if your library requires a specific PHP version, eg. PHP 7.3, you should add this to the composer.json.

	"require": {
		"php": ">=7.3"
	},

Run composer validate to make sure the changes to composer.json is valid.

Optional: Create a README.md file - which will be shown on the Packagist.org website.

You are now ready to publish your library!

Publishing to Packagist.org

  1. Git init and commit your files.

  2. Create a git tag, as it will be used by Composer. git tag -a 1.0.0 -m "First release" (It's recommended to use semantic versioning)

  3. Push the commit and tags to GitHub. (Tip: To always push tags when doing a normal push, run git config --global push.followTags true)

  4. Go to https://packagist.org/login and sign in using your GitHub account.

  5. Go to Submit and enter the GitHub URL of your repository.

Great, you should now have created your first Composer package. Try requiring it in another project and try it out.

<?php
require 'vendor/autload.php';
use [username]\MyData\MyData;

$mydata = MyData();
$mydata->set('works', true);
echo $mydata->get('works');

Updating Your Library

Remember to git tag a new version when you have committed a new release. Packagist will then automatically update your library with the new version.

Follow RSS/Atom Feed
See more posts