Codeigniter - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Library

CodeIgniter Library

shape Description

One of the basic and important feature of CodeIgniter is it has good set of built-in libraries that increases the performance of an application. The libraries folder is located in System folder. Any CodeIgniter Library from these set of libraries can be loaded whenever required. To load a built-in library, use the library function like below.
$this->load->library('class name');
Class name in the above syntax represents the CodeIgniter Library name that has to be loaded. To load an array of built-in libraries, use
$this->load->library(array('calendar', 'upload', 'zip'));

Creating Libraries

shape Description

CodeIgniter has the capacity of creating own libraries as per the need. These user defined libraries are stored in the folder application/libraries. These custom libraries can be created in 3 different ways.

Defining CodeIgniter Library

SpLibrary.php [php] <?php defined('BASEPATH') OR exit('No direct script access allowed'); class CI_SpLibraryClass { public function test1() { echo "<h1>This is test1 method in SPlessonsClass</h1>"; } public function test2() { return "<h1>This is test2 method in SPlessonsClass</h1>"; } } [/php]

Access Library class in a controller

From any of the controller in CodeIngiter library class can be accessed. The file here can be without an extension .php and class name can be accessed either in capital or small case. [php] $this->load->library('splessonsclass'); [/php]

Extend the Native Library

shape Description

There comes a need to add the necessary functionality to the built-in libraries given by the CodeIgniter. Here, the CodeIgniter Library can be extended and add the function by extending the class of native library class like shown below. [php] Class Splessons_Calendar extends CI_Calendar { } [/php] Actually, CI_Calendar is the native Library class and Splessons_Calendar is user-defined class. This extends the native Library class.

Replace the Native Library

shape Description

To create custom libraries that has own functionality, just replace the original/native Library class by giving the same class name as that of the class named in native library. For example, in order to replace the Calendar class, use the code as shown below and the save file as Calendar.php and name the class as CI_Calendar. Calendar.php [php] Class CI_Calendar { }[/php]

Summary

shape Key Points

  • Built-in libraries are stored in Systems folder.
  • Libraries can be created by extending or by replacing or by creating a new one in CodeIgniter.