One of the huge changes happened in PHP world is the Composer. With the Composer, you can able to use third party libraries in your project. But here I am trying to say that you are using other’s libraries in your project, but why not you also create a library and use that by others as well. This article will cover how to Create Package in PHP, let’s see!
How to Create Package in PHP?
Here I am creating a sample package step by step, so you can see and do it by yourself. First of all, login to your GitHub account and create a new repository called ‘first-php-package‘ .

After creating a repository you will see something like that and now copy that clone URL.
Open your terminal and run this command sudo git clone https://github.com/YOUR_GITHUB_USER_NAME/first-php-package.git
. Then open this project with your code editor. And create an src folder, composer.json file in project’s root directory. Open up the composer.json file and paste this code.
{ "name": "YOUR_GITHUB_USER_NAME/first-php-package", "description": "This is my first PHP package", "license": "MIT", "authors": [ { "name": "YOUR NAME", "email": "YOYUR EMAIL ADDRESS" } ], "require": { "php": ">=5.3.0" }, "autoload": { "psr-0": { "src\": "" } } }
Here first-php-package’ is the name of the package, Please don’t change the name cause this must be the same with the GitHub repository name. You can change the other’s metadata as well in the composer.json file. Once you are done with editing the composer.json file then run sudo composer install
command.
Go to src folder and create a Hello.php file and paste this code into Hello.php file.
<?php namespace src; class Hello { public function sayHello() { return ' This is my first php package'; } }
Then run sudo composer dump-autoload
command. And create a test.php file in your root directory and paste this code.
<?php require 'vendor/autoload.php'; $helloClass = new srcHello(); echo $helloClass->sayHello();
Finally, commit and push all the code, and browse your project (e.g.http://localhost/first-php-package/test.php)
Now you have the package but you still need to submit that package. Go yo packagist and login by your GitHub account. Then click the ‘Submit Package’ button, after that put your repository URL (in this case repository URL is https://github.com/YOUR_GITHUB_USER_NAME/first-php-package) then check and submit. Now your package is submitted to packagist and anyone can able to use it.
This is really basic way of creating a package and submit into packagist. Hope you can learn from this article how to create and how to submit a package.