Using PHP Libraries with CodeIgniter : Sample ACL Tutorial

Your library classes should be placed within your application/libraries folder, as this is where CodeIgniter will look for them when they are initialized.

File names must be capitalized. For example: Myclass.php
Class declarations must be capitalized. For example: class Myclass
Class names and file names must match.

The Class File

Classes should have this basic prototype (Note: the name Someclass purely as an example):

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

class Someclass {

    public function some_function()

/* End of file Someclass.php */

Using Your Class

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


Where someclass is the file name, without the “.php” file extension. You can submit the file name capitalized or lower case. CodeIgniter doesn’t care.

Once loaded you can access your class using the lower case version:

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

Passing Parameters When Initializing Your Class

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

$params = array('type' => 'large', 'color' => 'red');
$this->load->library('Someclass', $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');
class Someclass {
    public function __construct($params)
        // Do something with $params

Utilizing CodeIgniter Resources within Your Library
To access CodeIgniter’s native resources within your library use the get_instance() function. This function returns the CodeIgniter super object.

Normally from within your controller functions you will call any of the available CodeIgniter functions using the $this construct:


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

First, assign the CodeIgniter object to a variable:

$CI =& get_instance();

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

$CI =& get_instance();

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

$CI =& get_instance();

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

If you need to call a library (and its functions) within a view, you can do this:

$CI =& get_instance();

Please note That is one wrong approach to MVC although it is possible. You don’t need to load the library in the view, because all views are loaded from one CONTROLLER, so every external Helper or Library should be loaded from the controller and the used or send to the views.

On that base, we will implement under codeigniter an ACL process using a simple ACL library class downloaded from This class is called Easy_ACL.php. You can follow this tutorial HERE.


Since 30 years I work on Database Architecture and data migration protocols. I am also a consultant in Web content management solutions and medias protecting solutions. I am experienced web-developer with over 10 years developing PHP/MySQL, C#, VB.Net applications ranging from simple web sites to extensive web-based business applications. Besides my work, I like to work freelance only on some wordpress projects because it is relaxing and delightful CMS for me. When not working, I like to dance salsa and swing and to have fun with my little family.

You may also like...