Categories
Code Area PHP Programming Tools and Technologies

How to Create Package in PHP

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‘ .

 

HOW TO CREATE PACKAGE IN PHP
HOW TO CREATE PACKAGE IN PHP

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.

Categories
Tools and Technologies

Why Should Use Composer and How to Use It

One of the massive changes happened in PHP world is the Composer. You may be heard about composer but I don’t know either you use it or not. I am here to describe you why should we use composer, how much it is good for you and how to use it. Cheers!

What is Composer?

Composer is dependency management in PHP and it deals with various types of packages or libraries of PHP. Composer allows us to declare/write required PHP libraries/packages  for the project and it will install of those libraries/packages inside your project. And most important it will autoload all the installed  libraries/packages so that you can easily access these libraries/packages classes.

Why Should Use Composer?

We are using 3’rd party libraries/packages in our project. When we need a library then we download that library and store inside somewhere of our project. After that we require/include that library for access it’s classes. And we are doing same thing again and again when we need a another new library. That’s really annoying to a programmer or developer to download and include again and again, isn’t it? OK Composer comes to avoid this problem and provide a flexible way to manage all the packages or libraries inside your project. Together with Composer we have some rules and regulation, creating a PHP project with the same structure, using namespaces, the same code style, etc. For more information visit the composer official website https://getcomposer.org.

How to Use Composer?

Using of composer is really easy. Before you use Composer in your project your need to install Composer in your computer. By the end of this article, you will be able to plug and play with chunks of code in any framework, whether you work with CodeIgniter, FuelPHP, Laravel, Symfony2,Lithium, Yii, Zend… or anything else.

 

Installing Composer:

Installation of composer into you machine is really easy. Here I am showing the installation of composer for Ubuntu (Linux) operating system. If you are using Windows then see here.  Open you terminal and run this commands.

 

$ curl -s https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

 

Setup Project With Composer:

To start using Composer in your project, you need to create composer.json file in your project’s root directory. And write the dependencies of your project (may also contain other metadata as well) in this file. The JSON format is pretty easy to write. It allows you to define nested structures.

With the ‘require’ keyword you can telling Composer which packages you will need to this project. And Composer will download all the packages from Packagist.

{
    "require": {
        "monolog/monolog": "1.0.*"
    }
}

 

Here you can see I have required ‘monolog/monolog’ library with the version ‘1.0’ by using ‘require keyword. Here ‘monolog/monolog’ is the package name and ‘1.0’ is the version of this library.  Go to Packagist and you will get lot’s of php library you wanna need. Packagist is a repository Composer packages. Here you can find packages and lets Composer know where to get the code from.

Now just run ‘Install‘ command to get all the packages in your local project.

 

php composer.phar install

 

After run this command composer will find the ‘monolog/monolog’ library in Packagist and download the latest version in store into ‘vendor’ directory. And you will see a composer.lock file is create in your directory. Now go to created ‘vendor’ directory here you will see a ‘autoload.php’ file as well. You just need to require/include this class to using ‘monolog/monolog’.

 

require 'vendor/autoload.php';

 

Now you can easily access the ‘monolog/monolog’ classes in your project. Here I am writing a sample code so you can get some idea.

$log = new MonologLogger('name');

$log->pushHandler(new MonologHandlerStreamHandler('LOG_FILE_PATH/error.log', MonologLogger::WARNING));

$log->addWarning('Foo');

So these are very basic way to manage your project via Composer. To know the more details visit the composer official website https://getcomposer.org. I hope now you enjoy this tutorial and quite able to use composer. Best of luck for using Composer.