Codeigniter Create Own Libraries
Yes, you can also create your own libraries in codeigniter, however, we will instead describe how you can create your own libraries within your application/libraries directory in order to maintain
the separation between your native resources and therefore the international framework resources.
Somethings can do:
- You can create entirely new libraries.
- You can extend native libraries.
- You can replace native libraries.
Note - The info categories can't be extended or replaced along with your own categories.
All other classes are able to be replaced/extended.
Library Naming Conventions
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.
Codeigniter Class File: Prototype
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Someclass {
public function some_method()
{
//Some Code here
}
}
How to use custom library
Where some class is the file name, without the “.php” file extension. You can submit the file name capitalized or lower case.
Once loaded you'll access your category mistreatment the lower-case letter version:
How to pass parameters in the custom library?
In the library loading technique, you'll dynamically pass knowledge as an associate array via the second parameter and it'll be passed to your category constructor:
<?php
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('someclass', $params);
Next Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Someclass {
public function __construct($params)
{
// Do something with $params
}
}
Simply produce a config file named identically to the category file name and store it in your application/config/ directory.
Utilizing CodeIgniter Resources within Your Library
To access CodeIgniter’s native resources among your library use the get_instance() methodology.
This method returns the CodeIgniter super object.
Normally from among your controller ways, you may decide any of the on the market CodeIgniter ways mistreatment the $this construct:
<?php
$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');
$this, however, only works directly inside your controllers, your models, or your views.
If you'd prefer to use CodeIgniter’s categories from inside your own custom categories you'll do therefore 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();
<?php
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
// etc.
This is very important.
Assigning by reference permits you to use the first CodeIgniter object instead of making a replica of it.
However, since a library may be a category, it might be higher if you're taking full advantage of the OOP principles.
So, so as to be able to use the CodeIgniter super-object altogether of the category ways, you’re inspired to assign it to a property instead:
<?php
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');
}
}
How to replace Native Libraries with Your Versions?
Simply by naming your category files identically to a native library can cause CodeIgniter to use it rather than the native one.
To use this feature you want to name the file and therefore the category declaration precisely the same because of the native library.
For example, to exchange the native Email library you’ll produce a file named application/libraries/Email.php, and declare your class with:
<?php
class CI_Email {
}
To load your library you’ll see the quality loading method:
<?php
$this->load->library('email');
?>
How to extend native libraries?
If all you would like to try {and do} is add some practicality to an existing library - maybe add a technique or 2 - then it’s overkill to switch the whole library
with your version.
In this case, it’s higher to easily extend the category.
Extending a category is almost similar to substitution a category with a few exceptions:
The class declaration must extend the parent class.
Your new category name and computer file name should be prefixed with the mollusk genus (this item is configurable.
See below.).
For example, to increase the native Email category you’ll produce a file named application/libraries/MY_Email.php, and declare your category with:
<?php
class MY_Email extends CI_Email {
}