Home » CodeIgniter Library

CodeIgniter Library

by Online Tutorials Library

CodeIgniter Library

What is a Library

CodeIgniter provide a rich set of libraries. It is an essential part of CodeIgniter as it increases the developing speed of an application. It is located in the system/library.

Loading Library

CodeIgniter library can be loaded as following,

Here, class name should be replaced by the name of the library.

To load multiple libraries, use following code,

Creating Libraries

All the CodeIgniter libraries are placed in system folder. But in case if you want to use any other library in your application you can create it. There is no limitation for libraries. But your created libraries will be stored in the application/libraries folder. This is done to separate your local and global framework resources.

There are three methods to create a library,

  • Creating an entire new library
  • Extending native libraries
  • Replacing native libraries

Creating an entire new library

It should be placed in application/libraries folder.

Naming Conventions

  • File name first letter has to be in uppercase letter, like Mylib.php
  • Class name first letter should also be in uppercase letter
  • File name and class name should be same.

Basic syntax:

Suppose your file name is Mylib.php, then syntax will be as follows,

Loading Mylib.php

It can be loaded with the following line,

Note: You can write library name in any one of the upper or lower case letter.

Accessing mylib.php

Once it is loaded, you can access your class using the lower case letter because object instances are always in lower case.

Extending Native Libraries

You can also add some extended functionality to a native library by adding one or two methods. It will replace the entire library with your version. So it is better to extend the class. Extending and replacing is almost identical with only following exceptions.

  • Class declaration must extend the parent class.
  • New class name and filename must be prefixed with MY_.

For example, to extend it to native Calendar, create a file MY_Calendar.php in application/libraries folder. Your class will be declared as,class MY_Calendar extends CI_Calendar}

Replacing Native Libraries

Naming the new file and class name same as the native one will cause the CodeIgniter new one instead of native one. File and class declaration should be exactly same as the native library.

For example, to replace native Calendar library, you’ll create a file Calendar.php in application/libraries. And your class will be,

You may also like