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.
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.
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.
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).
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
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
Look at the above snapshot, the child driver class in invoked with the function invoke2.