Codeigniter libraries means classes which located in libraries directory. so for create our own custom library in codeigniter we need to build classes and store class file in libraries directory full path of libraries directory will be “application/libraries”. It’s easy to create new/ custom library , replace native or extend native library in codeigniter.
Below in this tutorial we will discuss :-

1. Create new/custom libraries

2. Using new/custom libraries

3. Extend native libraries

1. Create new/custom libraries :- We need to create our own or custom library to add some new functionality.

Library rules / naming :-

1. New library file must be stored in “application/libraries” directory.
2. Library file names must be capitalized. like: Example.php
3. Class declarations in library must be capitalized. like: class Example
4. Library class name and library file name must match.

Class Example :- creating a “Example.php” file under “application/libraries/Example.php” and add below code.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Example {
  public function my_function() {
    // do your stuff
  }
}

2. Using new/custom libraries :- After create custom library let’s see how to use this library in controller/model/view.

Include or initialize library :- first you need to include library to use functionality to include call below function where you need. Need to pass filename(case insensitive) “example” without “.php” extension.

$this->load->library('example'); 
// or $this->load->library('Example'); 

or autoloading library by “applications/config/autoload.php” (will include for all pages)

$autoload['helper'] = array('example');

Passing parameters on class Initialization :- for this your class must have a constructor

$arr = array('name'=>'rakesh');
$this->load->library('example', $arr);
 
//class
 
class Example {
  public function __construct($arr) {
    // Do your stuff with $arr
  }
}

Calling library function :- after load you can access class function with Object instances will be lower case.

$this->example->my_function();

Passing parameter to function:-

$arr = array('name'=>'rakesh');
$this->example->my_function($arr);
// class function  :- public function my_function($arr){}

Use codeigniter resources in Library :- you can also use another library, config, helper etc. in your custom library with CI instance like

$CI =& get_instance();
$CI->load->helper('myhelper');
$CI->load->library('email');
$CI->config->item('myitem');

3. Extend native libraries :- extend means you are adding some more functionality to existing library. for extend native library your

1. library name must be start with MY_
2. Class must extend parent class.

First Add class prefix to config :- go to applications/config/config.php and add subclass_prefix

$config['subclass_prefix'] = 'MY_'; 

We are going to extend CI_Form_validation :- create a file MY_Form_validation.php under “application/libraries/MY_Form_validation.php” and adding new form validation

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
  // check for alpha and space
  function alpha_space($str) {
    return ( ! preg_match("/^([a-z ])+$/i", $str)) ? FALSE : TRUE;
  }
}

Now need to include this library :-

$this->load->library('form_validation');

and call this validation rule like:-

$this->form_validation->set_rules('name', Name', 'trim|required|xss_clean|alpha_space');