Javatpoint Logo
Javatpoint Logo

CodeIgniter Driver

Drivers are introduced in CodeIgniter 2.0 and onwards.


What are Drivers

These are special type of library that has a parent class and many child classes. These child classes have access to the parent class, but not to their siblings. It enables you to make more elegant classes and more elegant syntax inside your controller.

Drivers are found in system/libraries folder in CodeIgniter folder.


Initializing Driver

To initialize a driver, write the following syntax.

Here, class_name is the driver name you want to invoke.

For example, to invoke a driver class main_class, do the following.

To invoke its method,

And then child classes can be called directly through the parent class, without initializing them.


Creating Own Drivers

There are three steps to create a driver in CodeIgniter.

  1. Making file structure
  2. Making driver list
  3. Making driver(s)

Making file structure

Go to system/libraries folder of CodeIgniter and make a new folder My_driver. Inside this folder make a file My_driver.php.

Now make a new folder inside My_driver folder, name it as drivers. Inside this new folder make a file, My_driver_first_driver.php.

The following file structure will be shown.

Codeigniter Driver 1

In CodeIgniter, driver library structure is such that subclasses don?t extend and hence they don't inherit the properties or methods of the main driver (in this case it is My_driver).

  • My_driver - it is a class.
  • My_driver.php - Parent driver
  • My_driver_first_driver.php - Child driver

Making Driver List

In the file My_driver.php in system/libraries/My_driver folder write the following code,

In the file My_driver_first_driver.php in system/libraries/My_driver/drivers write the following code,

Make a controller file Mydrive.php in application/controllers with the following code,

On your browser run the URL, http://localhost/driver/index.php/mydrive/invoke1

Codeigniter Driver 2

Look at the above snapshot, the parent driver class in invoked with the function invoke1.

Now run the URL, http://localhost/driver/index.php/mydrive/invoke2

Codeigniter Driver 3

Look at the above snapshot, the child driver class in invoked with the function invoke2.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA