Sometimes we need some custom functionality for our requirement. Let’s see how we can create our own custom library.
Step 1: Create custom library
Create your custom Demo.php in “application/libraries/” folder.
class Demo
{
private $CI;
function __construct()
{
$this->CI = get_instance();
}
function show()
{
// Your logic
}
}
Step 2: Autoload your custom library
Codeigniter libraries are the classes located in system/libraries/ or your application/libraries/ directory.
If you want your libraries to be autoload always, the you can add your custom library in $autoload[‘libraries’] option array.
$autoload['libraries'] = array('demo');
// You can also supply an alternative library name:
// $autoload['libraries'] = array('demo' => 'custom_name');
Step 3: Use your custom library
Now, since your custom library already autoloaded, so now you don’t need to load your custom library in order to use.
// In any controller
$this->demo->show();
Share This News