How to Create & Publish a PHP Package with Composer?
Last Updated :
30 May, 2024
Creating and publishing a PHP package with Composer is a straightforward process that enables you to share your code with the PHP community. Composer, the popular dependency manager for PHP, makes it easy to manage your package and its dependencies.
Prerequisites
Ensure you have the following installed on your machine:
- PHP (version 7.4 or later)
- Composer (latest version)
- Git (latest version)
- A GitHub account
Steps to create and publish a PHP package with Composer
Step 1: Create a New Directory for Your Package
First, create a new directory for your PHP package and navigate into it:
mkdir my-php-package
cd my-php-package
Step 2: Initialize a New Composer Package
Initialize a new Composer package by running:
composer init
This command will prompt you to fill in several details about your package. Here are the typical prompts and example answers:
- Package name: your-username/my-php-package
- Description: A sample PHP package
- Author: Your Name <[email protected]>
- Minimum Stability: stable (or leave blank)
- Package Type: library
- License: MIT
For the dependencies, you can specify any required packages. For now, we'll keep it simple and just require PHP 7.4 or later:
{
"require": {
"php": ">=7.4"
}
}
Confirm and generate the composer.json file.
Step 3: Create the composer.json File
The composer init command creates a composer.json file, which should look something like this:
{
"name": "your-username/my-php-package",
"description": "A sample PHP package",
"type": "library",
"require": {
"php": ">=7.4"
},
"autoload": {
"psr-4": {
"MyPackage\\": "src/"
}
},
"authors": [
{
"name": "Your Name",
"email": "[email protected]"
}
],
"license": "MIT"
}
Step 4: Create the Source Directory and Add Code
Create a src directory and add your PHP code inside it. This example will create a simple class:
mkdir src
Inside the src directory, create a HelloWorld.php file with the following content:
<?php
namespace MyPackage;
class HelloWorld
{
public function sayHello(): string
{
return "Hello, World!";
}
}
Step 5: Write Tests (Optional but Recommended)
Testing your package is crucial. You can use PHPUnit, a popular testing framework for PHP.
First, install PHPUnit as a development dependency:
composer require --dev phpunit/phpunit
Create a tests directory and a HelloWorldTest.php file:
mkdir tests
In tests/HelloWorldTest.php, write a simple test for your class:
<?php
use PHPUnit\Framework\TestCase;
use MyPackage\HelloWorld;
class HelloWorldTest extends TestCase
{
public function testSayHello()
{
$helloWorld = new HelloWorld();
$this->assertEquals('Hello, World!', $helloWorld->sayHello());
}
}
Run your tests using:
vendor/bin/phpunit tests
Step 6: Initialize a Git Repository
Initialize a Git repository in your project directory:
git init
Create a .gitignore file to exclude certain files from your repository:
echo "vendor/" > .gitignore
echo ".DS_Store" >> .gitignore
echo "composer.lock" >> .gitignore
Commit your changes:
git add .
git commit -m "Initial commit"
Step 7: Push to GitHub
Create a new repository on GitHub. Do not initialize it with a README or any other files, as this will cause conflicts.
Link your local repository to the GitHub repository and push your code:
git remote add origin https://github.com/your-username/my-php-package.git
git branch -M main
git push -u origin main
Step 8: Register Your Package with Packagist
To make your package available to others via Composer, you need to register it on Packagist.
Go to Packagist and log in using your GitHub account.
- Click on the "Submit" button.
- Enter the URL of your GitHub repository and click "Check".
- If everything looks good, click "Submit".
Packagist will now automatically fetch updates from your GitHub repository whenever you push changes.
Step 9: Install and Use Your Package
Your package is now available on Packagist and can be installed in any PHP project using Composer.
Create a new PHP project and install your package:
composer require your-username/my-php-package
To use the package in your project, include the Composer autoloader and use your package's classes:
<?php
require 'vendor/autoload.php';
use MyPackage\HelloWorld;
$helloWorld = new HelloWorld();
echo $helloWorld->sayHello();
Conclusion
By following these steps, you have created a PHP package, published it using Composer, and made it available on Packagist for others to use. This process allows you to share your code with the PHP community and easily manage it across different projects.
Further Considerations
- Versioning: Use semantic versioning (semver) for your package versions.
- Documentation: Maintain a README.md file with usage instructions and examples.
- Continuous Integration: Set up CI/CD pipelines using services like GitHub Actions to automate testing and deployment.
Similar Reads
How to remove a package from Laravel using PHP Composer?
Laravel is a famous PHP framework, and it uses Composer to handle packages. Adding packages to the Laravel project is something you often do, but sometimes you need to remove them to keep your project organized. In this article, you will learn the step-by-step process to remove a package from Larave
2 min read
How to Build and Publish Python Packages With Poetry
Poetry is a modern and useful tool for package development and distribution in Python that helps with dependency management. The entire process is consolidated into a single, intuitive interface with Poetry, in contrast to conventional methods that call for numerous tools and intricate setups. This
6 min read
How to publish a ReactJS component to NPM ?
Follow these simple steps in order to publish your own ReactJS component to NPM. Step 1: Initial Setup In order to publish any ReactJS Component to npm (node package manager), first we have to create a React component in the React app. Following are the instructions for creating any react app. Creat
3 min read
How To Push A Docker Image To Amazon ECR?
We go over how to submit a Docker image to the Amazon Elastic Container Registry (ECR) in this tutorial. By offering a safe, scalable registry for storing and distributing Docker images inside the AWS ecosystem, Amazon ECR streamlines container management. To upload and maintain your containerized a
4 min read
How to Use Docker Compose With Jenkins
In the fast-paced world of software development, it is mainly ensured by automation of repetition and task persistence. This is where Docker Compose and Jenkins come into play. Docker Compose is a tool for defining and running multi-container Docker applications. On the other hand, Jenkins is a wide
8 min read
How to Test PHP Code With phpUnit?
Testing PHP code is a critical aspect of software development, ensuring that applications function as intended and maintain their integrity over time. PHPUnit stands out as a premier testing framework for PHP, empowering developers to create comprehensive test suites and validate their code effectiv
4 min read
How to Install PHP Composer on cPanel?
cPanel is a web hosting management system. cPanel provides a control panel that provides a nice user interface. It is the most reliable & site management system. Moreover, cPanel provides a dashboard where some web date files and MySQL files are present to help others out. A composer is a tool t
2 min read
How to enable PHP's openssl extension to install Composer ?
To perform the secure HTTPS transfers you will need to enable the openssl extension then you have to install the composer. In this article, we will cover the enabling openssl for XAMPP as well as WAMP one by one then we will install the composer. Enable openssl extension on XAMPP Enable openssl exte
3 min read
How to add API function to a simple PHP Page ?
Application Programming Interface is a system that contains a set of rules or protocols or tools which help in providing interaction between two applications or software by standard data access. It is very much similar to a web service used in developing web pages or mobile apps. One application can
4 min read
How to create a container using Podman?
The emergence of Podman as a powerful engine for containers without daemons has presented a very good alternative to Docker. Always having your Podman installation up to date means that you will have the latest features, bug fixes, and security enhancements. This guide will take you through the step
3 min read