CodeIgniter – Libraries

CodeIgniter – Libraries

The  essential  part  of  a  CodeIgniter  framework  is  its  libraries.  It  provides  a  rich  set  of libraries,  which  indirectly  increase  the  speed  of  developing  an  application.  The  system library is located at system/libraries. All we need to do is to load the library that we want to use. The library can be loaded as shown below:

$this->load->library('class  name');

Where  class  name  is  the  name  of  the  library  that  we  want  to  load.  If  we  want  to  load multiple libraries, then we can simply pass an array as argument to  library() function as shown below:

$this->load->library(array('email',  'table'));

Library Classes  

The  library  classes  are  located  in  system/libraries.  Each  class  has  various  functions  to simplify  the  developing  work.  Following  table  shows  the  names  of  the  library  class  and its description.


Creating Libraries  
CodeIgniter  has  rich  set  of  libraries,  which  you  can  find  in  system/libraries  folder  but CodeIgniter  is  not  just  limited  to  system  libraries,  you  can  create  your  own  libraries  too, which  can  be  stored  in  application/libraries  folder.  You  can  create  libraries  in  three ways.
·Create new library
·Extend the native library

·Replace the native library

Create New Library
While creating new library one should keep in mind, the following things:
·The name of the file must start with a capital letter e.g. Mylibrary.php
·The class name must start with a capital letter e.g. class Mylibrary
·The name of the class and name of the file must match.

Mylibrary.php

<?php  if  (  !  defined('BASEPATH'))  exit('No  direct  script  access  allowed'); class  Mylibrary  {
public  function  some_function()
{
}
}
/*  End  of  file  Mylibrary.php  */


Loading the Custom Library

The above library can be loaded by simply executing the following line in your controller.

$this->load->libraryſŧmylibraryŨƀ;

mylibrary  is  the  name   of  your  library  and   you   can   write  it   in  lowercase  as   well   as uppercase letters. Use the name of the library without .php” extension. After loading the library, you can also call the function of that class as shown below.

$this->mylibrary->some_function();

Extend the Native Library

Sometimes,  you  may  need  to  add  your  own   functionality  to  the  library  provided  by CodeIgniter.  CodeIgniter  provides  facility  by  which  you  can  extend  the  native library  and add  your  own  functions.  To  achieve  this,  you  must  extend  the  class  of  native  library class.  For  example  if  you  want  to  extend  the  Email  library  then  it  can  be  done  as  shown below:

Class  MY_Email  extends  CI_Email  {

}


Here,  in  the  above  example,  MY_Email class is extending the native library’s email class CI_Email.  This  library  can  be  loaded  by  the  standard  way  of  loading  email  library.  Save the above code in file My_Email.php


Replace the Native Library
In  some  situations,  you  do  not  want  to  use  the  native  library  the  way  it  works  and  want to  replace  it  with  your  own  way.  This  can  be  done  by  replacing  the  native  library.  To achieve  this,  you  just  need  to  give  the  same  class  name  as  it  is  named  in  native  library. For  example, if you  want to replace the  Email class, then  use the code  as shown below. Save your file name with Email.php and give a class name to CI_Email.

Email.php
Class  CI_Email  {

}





Comments