CodeIgniter Creating Extending Replacing Libraries
**Creating your own libraries:**
Key Architecture Takeaways
- ›Point 1: Full-stack architecture blueprint: CodeIgniter Creating Extending Replacing Libraries.
- ›Point 2: Covers MVC separation of concerns, routing pipelines, and database abstraction.
- ›Point 3: Modern ecosystems: Laravel 11/PHP 8.3, React 19, and Node.js LTS runtimes.
Modern Framework Landscape (2026)
Web engineering patterns have evolved: Laravel now utilizes PHP 8.2+ typed properties, Vite asset bundling, and strict Eloquent typing, while Node.js LTS supports native fetch and ESM modules out of the box.
#
Creating your own libraries:
You can easily create your own libraries. The first step in creating a library is to create a file inside the *System/application/libraries/* directory, with a relevant name, you should also take the naming conventions outlined in the style guide into account. The next step is to create a class inside this file, with the same name as your PHP file, but without the .php extension. You do not need to extend any classes when creating your own library.
<blockquote>While creating new library one should keep in mind, the following things −
- The name of the file must start with a capital letter e.g. Example_library.php - class name must start with a capital letter e.g. class Example_library - The name of the class and name of the file must match.
</blockquote>
A prototype of "Example_library.php"
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Example_library {
public function some_function() {
//your function body
//......
}
}
/* End of file Example_library.php */Utilizing CodeIgniter Resources within Your Library:
When using CodeIgniter, you load helpers, libraries, and other resources by using the super object $this->. However, when creating your own library, you will not have access to this unless you explicitly add it in by using the following function:
$CI =& get_instance();
Once you do this inside a function, you would use $CI in place of $this when utilizing CodeIgniter resources.
class Example_library {
protected $CI;
// We'll use a constructor, as you can't directly call a function
// from a property definition.
public function __construct()
{
// Assign the CodeIgniter super-object
$this->CI =& get_instance();
}
public function foo()
{
$this->CI->load->helper('url');
redirect();
}
public function bar()
{
echo $this->CI->config->item('base_url');
}
}Use your own Class:
You can simply load your library using load method in your application. for example
$this->load->library('example_library');
Extending core libraries:
To extend a core CodeIgniter library, first, create a file with the same name as the library you wish to extend, but add the MY_ prefix. For example, to extend the Session class, create a file called MY_Session.php and place it inside the *system/application/libraries *directory. Once this is done, you simply extend the core library as follows:
<?php class MY_Session extends CI_Session{
function MY_Session()
{
parent::CI_Session();
}
}
?>Loading Extending Libraries:
You load your extended library in exactly the same way as you load the core library.
$this->load->library('session');
Note that I do not include the MY_ prefix to load my extended library. There is no need to include the prefix at the time of loading.
Replacing core libraries:
Replacing core libraries is similar to extending them. Instead of using the MY_ prefix in your file name and class name, you drop the prefix for the filename and use the prefix CI_ for the class name. This is because all CodeIgniter core libraries have the prefix CI_. Staying with the session class, we would replace it as follows:
<?php class CI_Session{
// Your updated class..
}
?>Loading Replaced Library:
Loading method is similar as extending library.
$this->load->library('session');
Conclusion:
So you can create your own library for your application, and also customize any existing library as you wish in CodeIgniter with flexible way.
