Autoloading custom classes in Codeigniter

Edit: This is old. Install Composer instead, and require the vendor/autoload.php

I’ve been messing around with CodeIgniter recently. It’s not bad at all, and for non enterprise applications or small sites, it’s a good choice! However I wanted to bring across my own classes that I’d already wrote, so what we need to do is write a helper script to set up SPL Autoloading. All my classes were written using the conventions of Zend Framework, so rather than write my own autoload feature, I thought ‘Hmm, I could also use the validators, filters, form elements, and a couple of other awesome non MVC related stuff from ZF. ‘ So here’s how:

Create a file called zend_framework_helper.php

ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.BASEPATH."../application/third_party/");
 require_once 'Zend/Loader/Autoloader.php';
 spl_autoload_register(array('Zend_Loader_Autoloader', 'autoload'));

Copy your Zend Folder to¬†/application/third_party/ . Last thing you need to do is autoload the helper. Open config/autoload.php, find the helpers array, and add ‘zend_framework’ to it.

To get your own ZF_Style_Class to autoload, (my custom classes in this instance are namespaced DD), add the namespace to the Zend Autoloader. You can put this in its own helper or just put it after the spl_autoload_register command in the zf helper.


So now in my Codeigniter controllers, I can call my own classes, and that of Zend Framework! Remember to setView(new Zend View()) in your Form objects! Check it out:

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

 public function index()
   $button = new Zend_Form_Element_Button('Frankenstein');
   $form = new Zend_Form('formname'); 
   $form->setView(new Zend_View()); 
   echo 'this rocks!<br />'.$form; 
   $test = new DD_Test(); 
   echo $test->pullLever(); 

Screen Shot 2013-05-23 at 21.12.50

Now we have the power of the non MVC Zend Stuff at out disposal!

If you’re interested in Codeigniter check out the awesome series of videos by David Connelly of DC Radio Network (, introduction video below!