Code Igniter User Guide Version 1.4.0


Creating Libraries

When we use the term "Libraries" we are normally referring to the classes that are located in the libraries directory and described in the Class Reference of this user guide. In this case, however, we will instead describe how you can create your own libraries within your application directory in order to maintain separation between your local resources and the global framework resources.

Storage

In order for your libraries to be stored in your application folder you will need to create two directories in which to store them:

Anatomy of a Library

A class library consists of two components:

  1. An init file.
  2. A class file.

The Init File

An init file a small initialization file corresponding to each of your classes. The purpose of this file is to instantiate a particular class. Each init file must be named identically to your class file name, adding the "init_" prefix. For example, if your class is named myclass.php your init file will be named:

init_myclass.php

Within your init file you will place your initialization code. Here's an example of such code, using an imaginary class named Myclass:

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

if ( ! class_exists('Myclass'))
{
     require_once(APPPATH.'libraries/Myclass'.EXT);
}

$obj =& get_instance();
$obj->myclass = new Myclass();
$obj->ci_is_loaded[] = 'myclass';

?>

The Class File

Your class file itself will be placed inside your libraries directory:

application/libraries/myclass.php

The class will have this basic prototype:

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

class Myclass {

}
?>

Naming Conventions

Using Your Class

From within any of your Controller classes you can initialize your class using the standard:

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

Once loaded you can access your class using:

$this->myclass->function();

Note: In your init file you can define the object variable name.

Passing Parameters Your Class

In the library loading function you can pass data via the second parameter and it will be available to your initialization file:

$params = array('type' => 'large', 'color' => 'red');

$this->load->library('myclass', $params);

Parameters will be accessible using a variable called $params. By default this variable is set to FALSE.

Utilizing Code Igniter Resources within Your Library

To access Code Igniter's native resources within your library use the get_instance() function. This function returns the Code Igniter super object.

Normally, to call any of the available Code Igniter functions requires you to use the $this construct:

$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');
etc.

$this, however, only works directly within your controllers, your models, or your views. If you would like to use Code Igniter's classes from within your own custom classes you can do so as follows:

First, assign the Code Igniter object to a variable:

$obj =& get_instance();

Once you've assigned the object to a variable, you'll use that variable instead of $this:

$obj =& get_instance();

$obj->load->helper('url');
$obj->load->library('session');
$obj->config->item('base_url');
etc.

Note: You'll notice that the above get_instance() function is being passed by reference:

$obj =& get_instance();

This is very important. Assigning by reference allows you to use the original Code Igniter object rather than creating a copy of it.