Creating Libraries
Note: This page is an extension of the CodeIgniter User Guide page on the same topic. Please read the Creating Libraries page in addition to this page for complete information about this feature.
Init Files
In it's earlier versions, CodeIgniter used a small initialization file called an init file to instantiate a particular class. That feature was removed in CodeIgniter version 1.5.x which has caused difficulties with integrating some third-party classes such as ADODB. While the details of how to resolve this issue are worked out, we have decided to re-add the ability to use init files.
Note: A nod and a thank you go out to Greg MacLellan for this thread in the CodeIgniter forum.
An init file a small initialization file corresponding to one of your classes. The purpose of this file is to instantiate your class. Not every class you add to CoolBrew will need an init file, but in some cases, they are invaluable. CoolBrew will look for an init file when loading a library and use it if it finds it. If it does not find one, it will create one of its own automatically.
If you need to use an init file, you will need to create a folder called init inside your module folder or inside the system folder, depending on where you want to install the library. The 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';
?>
Naming Conventions
- All file names must be in lowercase: myclass.php and init_myclass.php
- Class names must be capitalize: class Myclass