Programming PHP How to create a custom library in CodeIgniter

How to create a custom library in CodeIgniter

Private: How To Create A Custom Library In CodeIgniter

When we develop a website in CodeIgniter, we need some customized code. Though CodeIgniter has a big collection of useful libraries. But sometimes we need to define some custom codes. Code reusability is very important whether it is a small scale website or a large one. To accomplish this purpose, we can create our own libraries additionally. This article is all about how to create a custom library in CodeIgniter

To create a custom library, we simply need to create a PHP class. Follow the steps mentioned below to create a custom library in CodeIgniter.
The name of the library will be myLibrary.
Steps to create a custom library in CodeIgniter.

#1 Open Library folder

Assuming that you are aware of the directory structure of CodeIgniter. To create your own library, open application/libraries folder. All the libraries defined by you will be residing in this library itself.

#2 Create a library file

In application/libraries folder, create a PHP file with name myLibrary.php. Now, to successfully create a library, you need to create a library class too.

#3 Create Library Class

Open the myLibrary.php file and create a class with the same name as file,i.e. myLibrary class. The code will be like I am mentioning below.

Doing this will create the library. However, it is useless until you put some code in it.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mylibrary {

}

#4 Put some code in your library

Let’s assume that this library will be used to calculate mathematical operations. So I am going to add a few methods to do addition and subtraction. Well, this is the kind of kids method, but my purpose is to explain the working.

So the first method will be plus() and the other one will be minus(). Both functions take two parameters to perform operations.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mylibrary {

    function plus($num1, $num2) {
        $sum = $num1 + $num2;
        return "<h2>Sum of $num1 and $num2 is: $sum</h2>";
    }

    function minus($large, $small) {
        return "<h2>Result of $large - $small is: " . ($large - $small) . "</h2>";
    }

}

#5 Load Custom Library in CodeIgniter

You can load a system library or a custom library in two ways. Either by autoload or by loading them in controller wherever required.

Whenever we load any library, firstly, it is searched in the system/libraries folder. If it is not found in that folder, it will then be searched in application/libraries folder.

Autoload custom Library in CodeIgniter

If the library is not to be used in most of the places and of specific use, do not put it in autoload. To autoload any library, simply add its name to #autoload array in application/config/autoload.php file. By autoloading any library, you will not need to load it in any other place manually. Below is the code to autoload Mylibrary library.

$autoload['libraries'] = array('mylibrary');
# OR you can autoload multiple libraries
$autoload['libraries'] = array('session', 'database','mylibrary');

Load custom Library in CodeIgniter manually

To load any library in CodeIgniter controller, we use CI_loader object $this. We can load any library using this method, whether it is a system library or a custom defined one. Below is the sample code to load a specific library. I am loading my defined library Mylibrary in Welcome controller. The Welcome controller is the default controller by CodeIgniter.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index() {
        $this->load->library('myLibrary');

        echo $this->mylibrary->plus(12, 15);

        echo $this->mylibrary->minus(200, 150);

    }
}

The output of the code above will be as shown in the image below.

Load custom Library in CodeIgniter

Leave a Reply