CodeIgniter Library

Next Page Previous Page

Codeigniter provides a rich set of libraries that make it faster and essential, all libraries are located in the system/libraries. We can simply use them by loading them in our code. It makes our work much faster and eliminates the need of rewriting the code several times for repeated and similar functionalities.

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

Class name here is the name of the library that we wish to use. And to use multiple libraries we can pass array in the library()

Example :  $this->load->library(array(‘library1’, 'library2'));

Creating Libraries:

 The experienced programmer always has their custom libraries ready with them, Codeigniter fulfills our wish to have one, all custom libraries are stored in the application/libraries folder we can simply add our library there without worrying about the application.

  • Create a New Library :

 While creating a library all we need to keep in mind is to name our library and class similar and that starts from capital letter 

Example: let’s create MyCustomLibrary

<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed here');
 
   class MyCustomLibrary {

      public function myFunction() {

      // Add your code here

      }

   }

 

Loading our library : $this->load->library(mycustomlibrary);

Calling a function from library : $this->mycustomlibrary>myfunction();

  • Extend the Native Library:

There will scenarios where we need to add our functions to native libraries 

in this case, we extend the native library and add our own set of codes.

 Example:

 

class MY_Input extends CI_Input{ function MY_Input(){ parent::CI_Input(); }

The CI_Input class is a core library , and MY_Input is what we are adding.
  • Replace the Native Library:

 In case we don’t want o use a native function as it is and we want to manipulate the outcome as per our requirements we can replace the code in native libraries by creating a class of the same name as the native library 

 Example: File Input.php

 

Class CI_Input { 

                        // We will add some code here.

}

We can use the above code to replace the Input class and save it as  Input.php

 

Next Page Previous Page

Treding Tutorial

Popular Tutorials

to Learn now