CodeIgniter Creating Extending Replacing Libraries

Creating your own libraries:

You can easily create your own libraries. The first step in creating a library is to create a file inside the System/application/libraries/ directory, with a relevant name, you should also take the naming conventions outlined in the style guide into account.
The next step is to create a class inside this file, with the same name as your PHP file, but without the .php extension. You do not need to extend any classes when creating your own library.

While creating new library one should keep in mind, the following things −

  1. The name of the file must start with a capital letter e.g. Example_library.php
  2. class name must start with a capital letter e.g. class Example_library
  3. The name of the class and name of the file must match.

 A prototype of “Example_library.php”

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
 class Example_library {

 
       public function some_function() {


       //your function body
       //......

      }
 }
 
/* End of file Example_library.php */

 Utilizing CodeIgniter Resources within Your Library:

When using CodeIgniter, you load helpers, libraries, and other resources by using the super object $this->. However, when creating your own library, you will not have access to this unless you explicitly add it in by using the following function:
$CI =& get_instance();

Once you do this inside a function, you would use $CI in place of $this when utilizing CodeIgniter resources.

 class Example_library {

 protected $CI;

 // We'll use a constructor, as you can't directly call a function
 // from a property definition.
 public function __construct()
 {
      // Assign the CodeIgniter super-object
      $this->CI =& get_instance();
 }

 public function foo()
 {
     $this->CI->load->helper('url');
     redirect();
 }

 public function bar()
 {
     echo $this->CI->config->item('base_url');
 }

}

 Use your own Class:

You can simply load your library using load method in your application. for example

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

Extending core libraries:

To extend a core CodeIgniter library, first, create a file with the same name as the library you wish to extend, but add the MY_ prefix. For example, to extend the Session class, create a file called MY_Session.php and place it inside the
system/application/libraries directory. Once this is done, you simply extend the core library as follows:

<?php  class MY_Session extends CI_Session{

    function MY_Session()
    {
        parent::CI_Session();
    }
 }
?>

Loading Extending Libraries:

You load your extended library in exactly the same way as you load the core library.

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

Note that I do not include the MY_ prefix to load my extended library. There is no need to include the prefix at the time of loading.

Replacing core libraries:

Replacing core libraries is similar to extending them. Instead of using the MY_ prefix in your file name and class name, you drop the prefix for the filename and use the prefix CI_ for the class name. This is because all CodeIgniter core libraries have the prefix CI_. Staying with the session class, we would replace it as follows:

<?php class CI_Session{


// Your updated class..


}
?>

Loading Replaced Library:

Loading method is similar as extending library.

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

Conclusion:

So you can create your own library for your application, and also customize any existing library as you wish in CodeIgniter with flexible way.

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

One Comment

  • When you create MY_session, you say you need to put it in the
    system/application/libraries
    Are you sure this is the correct directory?

    When I put
    log_message(‘debug’, ‘In MY_Session-library before tests’);
    inside the MY_session.php file, it never appears in the log files, and I dont know if my_session is really working or not.

    Can you perhaps help with this?

    Thanks very much,

    Eli

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.