Code Igniter User Guide Version 1.5.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

Your library classes should be placed within your application/libraries folder, as this is where Code Igniter will look for them when they are initialized. If your class is named identically to a native class from the system/libraries folder, your version will be used instead.

Naming Conventions

The Class File

Classes should have this basic prototype (Note: We are using the name Myclass purely as an example):

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

// Initialize the class
$obj =& get_instance();
$obj->init_class('Myclass');

class Myclass {

    function some_function()
    {
    }
}

?>

You'll notice in the above example that the class is instantiated directly from the file itself using these two lines of code:

$obj =& get_instance();
$obj->init_class('Myclass');

Make sure and submit your class name in the first parameter of the $obj->init_class() function. In the above example it is Myclass

Using Your Class

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

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

Where Myclass is the file name, without the ".php" file extension. You can submit the file name capitalized or lower case. Code Igniter doesn't care.

Once loaded you can access your class using:

$this->myclass->some_function();  // Object instances will always be lower case

Setting a Different Class Variable Name

If you would like the object variable ($this->myclass) set to a different name you can specify it when initializing your class. For example, let's initialize it as foobar:

$obj =& get_instance();
$obj->init_class('Myclass', 'foobar');

In the above example you would still load your class like this:

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

But you would use it like this:

$this->foobar->function();

Passing Parameters When Initializing Your Class

In the library loading function you can dynamically pass data via the second parameter and it will be passed to your class constructor:

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

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

If you use this feature you must set up your class constructor to expect data:

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

// Initialize the class
$obj =& get_instance();
$obj->init_class('Myclass');

class Myclass {

    function Myclass($params)
    {
        // Do something with $params
    }
}

?>

You can also pass parameters via the third parameter of the $obj->init_class() function:

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

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

// Initialize the class
$obj =& get_instance();
$obj->init_class('Myclass', 'myclass', $params);

class Myclass {

    function Myclass($params)
    {
        // Do something with $params
    }
}

?>

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 from within your controller functions you will call any of the available Code Igniter functions using 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.