PHP: Creating Your First Composer Package
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
-
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) - Description - Enter a short description of your package.
-
Author - Defaults to your
git config user.name
andgit config user.email
. -
Package Type - Enter
library
- License - For now just leave it empty, but later check out choosealicense.com
- 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
-
Git init and commit your files.
-
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) -
Push the commit and tags to GitHub. (Tip: To always push tags when doing a normal push, run
git config --global push.followTags true
) -
Go to https://packagist.org/login and sign in using your GitHub account.
-
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.