Extending or replacing core classes in CodeIgniter 2x

It has been quite some time that CodeIgniter 2x has been released after a quite long waiting. There are several improvements in this latest release and upgrading from the 1x is quite easy. However, they have changed some file/folder organization in this latest release. I found these changes as good as it provides better application wise personalization. CodeIgniter’s previous file/folder structures were not (at least, by default) very good (without some/little modification) for hosting multiple application based on single core installation. However, in this version they organized it better.

On of the major change was made in this version in replacing and/or extending core classes. There was no note in their upgrade guide (but explains in their relevant doc page, which I found later). So I had to go through little trouble with it until Arafat Rahman helped me out!

However, the process is simple. In version 2x, they have introduced a new directory called ‘core’ (along with few others). All core classes that needs to be replaced or extended should be in this directory. I liked it very much, as I hated to put core classes in library directory in older versions.

Replacing a core class
Let’s say we want to replace a core class by our own version. Also let’s assume we want to replace the model class (actual name is CI_Model). Now follow these steps:

  • Create a directory named ‘core’ in your application directory, if it does not exist. If your application directory is default one, it would be ‘application/core’
  • Create a new file named Model.php.
  • Put the following line in the newly created empty file:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class CI_Model
 function __construct()

Now when you create model classes in current application extend the CI_Model as you were doing earlier, this class will be extended rather than the CI_Model class in the system/core/Model.php. However, when you do, think if you have enough reasons to replace the core class as you can do most, if not all, of the tasks by simply extending it.

Extending the core class

Rather than replacing, I find it much better to extend the core class and use that class in my application. The steps of extending the core class are also simple.

  • Create a ‘core’ directory in your application directory (example: application/core), if it does not exist.
  • Create a new file named like PREFIX_Model.php. The PREFIX should be replaced by

    setting in your config.php file. By default, the subclass prefix is ‘MY_’. So the file should be like ‘MY_Model.php’. I change it in each application though :-).

  • Now, inside the file create your own class prefixed with same $config[‘subclass_prefix’] setting and extending the CI_Model class. For example it can be :
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Model  extends CI_Model{

  • Do not forget to the constructor of parent class.
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Model  extends CI_Model{

 function __construct(){


Major part is done. However, your application will not know about this class unless you extend it in your application. Earlier you would extend CI_Model in your application (like: class User_model extends CI_Model) but from now on you need to extend the newly created class like

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


Unless you had some overridden and/or added methods and/or properties in your newly created class, there is no justification of overriding the parent class. What I do is always keep some common methods in the overridden model class (mostly database related methods) that might be used in almost all sections (and does not belong to any special section) of the application. This allows me to work without loading the same thing repeatedly.

I hope you find this useful. Cheers!

  • Thanks for the tutorial. This tutorial describes the things very well.

  • Thanks, this was very helpful. I was looking for this for a long time. Advice on the CI forum on how to show custom 404 when disallowed URI chars are entered were outdated and didn’t work with CI 2.0.


  • Jayesh Ambali

    I tried the exact stuff, but it did not work for me as I’m not able to use the database functions like $this->db->get(). set() etc, I’m getting an error like this – Undefined property: Accounts::$db

  • Jayesh Ambali

    Later I figured out the issue, it was because, I wasn’t loaded database class in autoload.php file.

  • cotton

    Thank for your answers,
    I got another case, I’d like to extend CI_Model in 2 subclasses, ex: MY_A_Model extends CI_Model and MY_B_Model extends CI_Model, how can I do this? or it’s limited to extend CI core class in only one sub class?

  • The HungryCoder

    That sounds an interesting question. I’ve no idea about it. Are you trying to extend the CI_Model twice in same application? Looks like something unusual 🙂