×

CodeIgniter Creating Library

As we know, CodeIgniter has a large collection of library functions that are stored in the system/libraries folder. CodeIgniter also provides some additional functionality in library files. For example, if you want to create custom library files, it allows you to create and save it in the application/libraries folder.

CodeIgniter provides three ways to create library files in the application,

  • Create a new library
  • Extend a native library
  • Replace a native library

Create a new library

You must remember that when you are creating a new library file, it should be placed in the application/library folder. And you also have to remember these things while creating libraries files such as:

  • The first letter of the library file name must be in uppercase e.g., Mainlibrary.php
  • The class name of a library file must be in uppercase letter class e.g., class Mainlibrary
  • The name of the class and library file must match.

Mainlibrary.php

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Mainlibrary {
         public function show_function()
         {
         }
 } 

Loading a library file in the controller

After creating a mainlibrary.php file, you should load it into the controller file by following the syntax:

$this->load -> library( ‘Mainlibrary’);

In the above Syntax, ‘Mainlibrary’ is the name of library file. The name of the library file can be in uppercase as well as a lowercase letter. You have to load a library file without a ‘.php’ extension in the controller. Once the library file is loaded in the controller, then you can call any methods of that class by following methods as shown below.

$this->Mainlibrary->show_function();

Extend a native library

If you want to add functionality to an existing library, CodeIgniter provides a native library. In this library, you can extend a native library class and add one or more methods to it. To achieve the same, you should extend an existing library class. The following points must be remembering:

  • The declaration of a new library class should extend the predefined base class in CodeIgniter.
  • MY_ should be used as a prefix in the library's class and file name.

Example: This is the method to extend a native session library class. You have to create a MY_Session native library class and save it in the application/libraries/MY_Session.php,

MY_Session.php

class MY_Session extends CI_Session {
             public function _construct ( $config = array() )
             {
                         parent:: _contsruct ( $config );
             }
             public function display()
 {
             }
 } 

Replace a native library

CodeIgniter also provides some flexibility, such as if you don't want to use the native library classes, you can replace it with your new library files. To achieve the functionality, you must declare the same name as the library class and file that is the same as the original library class in CodeIgniter. For example, if you want to change a native cache library, you have to create a class name ‘CI_Cache’ and save the ‘Cache.php’ file to application/library/Cache.php.

Cache.php

class  CI_Cache {
 } 


Related Topics

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

Update a Record CodeIgniter

Update a record In this topic, we will learn how we can update existing records in database tables. To update a record, CodeIgniter provides an update() function used with the set()...

10 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.

CodeIgniter URI Class

URI Class The CodeIgniter contains a URI class that is used to retrieve information from URI strings. It also provides suitability to get information from the re-routed segments. The system automatically initializes this...

6 minutes read.

CodeIgniter Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and...

4 minutes read.

CodeIgniter Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

8 minutes read.

CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

5 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

CodeIgniter Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

CodeIgniter Text Helper

The text helper contains the various function that is used to operate with text such as define a word limit and characters limit, conversion of word from ASCII to entities, etc. Loading the text...

4 minutes read.

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.