Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ######################### |
| 2 | Using CodeIgniter Drivers |
| 3 | ######################### |
| 4 | |
| 5 | Drivers are a special type of Library that has a parent class and any |
| 6 | number of potential child classes. Child classes have access to the |
| 7 | parent class, but not their siblings. Drivers provide an elegant syntax |
| 8 | in your :doc:`controllers <controllers>` for libraries that benefit |
| 9 | from or require being broken down into discrete classes. |
| 10 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 11 | Drivers are found in the *system/libraries/* directory, in their own |
| 12 | sub-directory which is identically named to the parent library class. |
| 13 | Also inside that directory is a subdirectory named drivers, which |
| 14 | contains all of the possible child class files. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 15 | |
| 16 | To use a driver you will initialize it within a controller using the |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 17 | following initialization method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 18 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 19 | $this->load->driver('class_name'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 20 | |
| 21 | Where class name is the name of the driver class you want to invoke. For |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 22 | example, to load a driver named "Some_parent" you would do this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 23 | |
| 24 | $this->load->driver('some_parent'); |
| 25 | |
| 26 | Methods of that class can then be invoked with:: |
| 27 | |
| 28 | $this->some_parent->some_method(); |
| 29 | |
| 30 | The child classes, the drivers themselves, can then be called directly |
| 31 | through the parent class, without initializing them:: |
| 32 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 33 | $this->some_parent->child_one->some_method(); |
| 34 | $this->some_parent->child_two->another_method(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | |
| 36 | Creating Your Own Drivers |
| 37 | ========================= |
| 38 | |
| 39 | Please read the section of the user guide that discusses how to :doc:`create |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 40 | your own drivers <creating_drivers>`. |