In this section, we will create a PHP package in GitHub and release it into packagist.org.
- Create a new folder, which is the name will become your package name. Example
calculator
. - Create a new folder inside
calculator
, with namesrc
. Your file should be stored /li> - Create file
Calculator.php
with classCalculator
, like below1234567891011121314151617181920212223242526<?phpnamespace redzjovi\calculator;class Calculator{/*** @param integer $number1* @param integer $number2* @return integer*/public function plus($number1, $number2){return ($number1 + $number);}/*** @param integer $number1* @param integer $number2* @return integer*/public function minus($number1, $number2){return ($number1 - $number);}}
- Create
composer.json
in your top folder (calculator/composer.json
) - With
composer init
or manually like below12345678910111213141516171819202122{"name": "redzjovi/calculator","description": "My first packagist package","type": "package","require": {"php": ">=5.3.0"},"license": "mit","authors": [{"name": "Jovi","email": "redzjovi@gmail.com","homepage": "http://github.com/redzjovi/calculator"}],"minimum-stability": "dev","autoload": {"psr-4": {"redzjovi\\calculator\\": "src/"}}}
The important things, your namespace (redzjovi\calculator
) in foldersrc
should be autoloaded. - Run
composer update
. Then create foldertest
and filetest.php
to test the class.123456789<?phprequire_once __DIR__ . '/../vendor/autoload.php';use redzjovi\calculator\Calculator;$calculator = new Calculator();$number = $calculator->plus(1, 2);var_dump($number); - After test complete, create
README.md
, commit and push to your GitHub.123456789101112131415## SynopsisPHP calculator.## Installation```composer require redzjovi/calculator```## How to use```use redzjovi\calculator\Calculator;$calculator = new Calculator();$number = $calculator->plus(1, 2);``` - The url git should be located inĀ
https://github.com/redzjovi/calculator.git
. - Go to packagist.org, create account and try to submit package.
- If your package is accepted, it will be show like this.
- The last is set auto update package on every push with Webhooks GitHub. Go to settings -> integration & services, add services packagist.
Add user, token from packagist, then submit.
Reference :
https://github.com/redzjovi/calculator