Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################## |
| 2 | Creating Libraries |
| 3 | ################## |
| 4 | |
| 5 | When we use the term "Libraries" we are normally referring to the |
| 6 | classes that are located in the libraries directory and described in the |
| 7 | Class Reference of this user guide. In this case, however, we will |
| 8 | instead describe how you can create your own libraries within your |
| 9 | application/libraries directory in order to maintain separation between |
| 10 | your local resources and the global framework resources. |
| 11 | |
| 12 | As an added bonus, CodeIgniter permits your libraries to extend native |
| 13 | classes if you simply need to add some functionality to an existing |
| 14 | library. Or you can even replace native libraries just by placing |
| 15 | identically named versions in your application/libraries folder. |
| 16 | |
| 17 | In summary: |
| 18 | |
| 19 | - You can create entirely new libraries. |
| 20 | - You can extend native libraries. |
| 21 | - You can replace native libraries. |
| 22 | |
| 23 | The page below explains these three concepts in detail. |
| 24 | |
| 25 | .. note:: The Database classes can not be extended or replaced with your |
| 26 | own classes. All other classes are able to be replaced/extended. |
| 27 | |
| 28 | Storage |
| 29 | ======= |
| 30 | |
| 31 | Your library classes should be placed within your application/libraries |
| 32 | folder, as this is where CodeIgniter will look for them when they are |
| 33 | initialized. |
| 34 | |
| 35 | Naming Conventions |
| 36 | ================== |
| 37 | |
| 38 | - File names must be capitalized. For example: Myclass.php |
| 39 | - Class declarations must be capitalized. For example: class Myclass |
| 40 | - Class names and file names must match. |
| 41 | |
| 42 | The Class File |
| 43 | ============== |
| 44 | |
| 45 | Classes should have this basic prototype (Note: We are using the name |
| 46 | Someclass purely as an example):: |
| 47 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 48 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 49 | |
| 50 | class Someclass { |
| 51 | |
| 52 | public function some_function() |
| 53 | { |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* End of file Someclass.php */ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 58 | |
| 59 | Using Your Class |
| 60 | ================ |
| 61 | |
| 62 | From within any of your :doc:`Controller <controllers>` functions you |
| 63 | can initialize your class using the standard:: |
| 64 | |
| 65 | $this->load->library('someclass'); |
| 66 | |
| 67 | Where *someclass* is the file name, without the ".php" file extension. |
| 68 | You can submit the file name capitalized or lower case. CodeIgniter |
| 69 | doesn't care. |
| 70 | |
| 71 | Once loaded you can access your class using the lower case version:: |
| 72 | |
| 73 | $this->someclass->some_function(); // Object instances will always be lower case |
| 74 | |
| 75 | Passing Parameters When Initializing Your Class |
| 76 | =============================================== |
| 77 | |
| 78 | In the library loading function you can dynamically pass data as an |
| 79 | array via the second parameter and it will be passed to your class |
| 80 | constructor:: |
| 81 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 82 | $params = array('type' => 'large', 'color' => 'red'); |
| 83 | |
| 84 | $this->load->library('Someclass', $params); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 85 | |
| 86 | If you use this feature you must set up your class constructor to expect |
| 87 | data:: |
| 88 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 89 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 90 | |
| 91 | class Someclass { |
| 92 | |
| 93 | public function __construct($params) |
| 94 | { |
| 95 | // Do something with $params |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | ?> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | |
| 101 | You can also pass parameters stored in a config file. Simply create a |
| 102 | config file named identically to the class file name and store it in |
| 103 | your application/config/ folder. Note that if you dynamically pass |
| 104 | parameters as described above, the config file option will not be |
| 105 | available. |
| 106 | |
| 107 | Utilizing CodeIgniter Resources within Your Library |
| 108 | =================================================== |
| 109 | |
| 110 | To access CodeIgniter's native resources within your library use the |
| 111 | get_instance() function. This function returns the CodeIgniter super |
| 112 | object. |
| 113 | |
| 114 | Normally from within your controller functions you will call any of the |
| 115 | available CodeIgniter functions using the $this construct:: |
| 116 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 117 | $this->load->helper('url'); |
| 118 | $this->load->library('session'); |
| 119 | $this->config->item('base_url'); |
| 120 | // etc. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | |
| 122 | $this, however, only works directly within your controllers, your |
| 123 | models, or your views. If you would like to use CodeIgniter's classes |
| 124 | from within your own custom classes you can do so as follows: |
| 125 | |
| 126 | First, assign the CodeIgniter object to a variable:: |
| 127 | |
| 128 | $CI =& get_instance(); |
| 129 | |
| 130 | Once you've assigned the object to a variable, you'll use that variable |
| 131 | *instead* of $this:: |
| 132 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 133 | $CI =& get_instance(); |
| 134 | |
| 135 | $CI->load->helper('url'); |
| 136 | $CI->load->library('session'); |
| 137 | $CI->config->item('base_url'); |
| 138 | // etc. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | |
| 140 | .. note:: You'll notice that the above get_instance() function is being |
| 141 | passed by reference:: |
| 142 | |
| 143 | $CI =& get_instance(); |
| 144 | |
| 145 | This is very important. Assigning by reference allows you to use the |
| 146 | original CodeIgniter object rather than creating a copy of it. |
| 147 | |
| 148 | Replacing Native Libraries with Your Versions |
| 149 | ============================================= |
| 150 | |
| 151 | Simply by naming your class files identically to a native library will |
| 152 | cause CodeIgniter to use it instead of the native one. To use this |
| 153 | feature you must name the file and the class declaration exactly the |
| 154 | same as the native library. For example, to replace the native Email |
| 155 | library you'll create a file named application/libraries/Email.php, and |
| 156 | declare your class with:: |
| 157 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 158 | class CI_Email { |
| 159 | |
| 160 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 161 | |
| 162 | Note that most native classes are prefixed with CI\_. |
| 163 | |
| 164 | To load your library you'll see the standard loading function:: |
| 165 | |
| 166 | $this->load->library('email'); |
| 167 | |
| 168 | .. note:: At this time the Database classes can not be replaced with |
| 169 | your own versions. |
| 170 | |
| 171 | Extending Native Libraries |
| 172 | ========================== |
| 173 | |
| 174 | If all you need to do is add some functionality to an existing library - |
| 175 | perhaps add a function or two - then it's overkill to replace the entire |
| 176 | library with your version. In this case it's better to simply extend the |
| 177 | class. Extending a class is nearly identical to replacing a class with a |
| 178 | couple exceptions: |
| 179 | |
| 180 | - The class declaration must extend the parent class. |
| 181 | - Your new class name and filename must be prefixed with MY\_ (this |
| 182 | item is configurable. See below.). |
| 183 | |
| 184 | For example, to extend the native Email class you'll create a file named |
| 185 | application/libraries/MY_Email.php, and declare your class with:: |
| 186 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 187 | class MY_Email extends CI_Email { |
| 188 | |
| 189 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 190 | |
Andrey Andreev | 1d57197 | 2012-03-07 11:49:35 +0200 | [diff] [blame] | 191 | If you need to use a constructor in your class make sure you |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 192 | extend the parent constructor:: |
| 193 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 194 | class MY_Email extends CI_Email { |
| 195 | |
Andrey Andreev | 1d57197 | 2012-03-07 11:49:35 +0200 | [diff] [blame] | 196 | public function __construct($config = array()) |
| 197 | { |
| 198 | parent::__construct($config); |
| 199 | } |
| 200 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 201 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 202 | |
Andrey Andreev | 1d57197 | 2012-03-07 11:49:35 +0200 | [diff] [blame] | 203 | .. note:: |
| 204 | Not all of the libraries have the same (or any) parameters |
| 205 | in their constructor. Take a look at the library that you're |
| 206 | extending first to see how it should be implemented. |
| 207 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 208 | Loading Your Sub-class |
| 209 | ---------------------- |
| 210 | |
| 211 | To load your sub-class you'll use the standard syntax normally used. DO |
| 212 | NOT include your prefix. For example, to load the example above, which |
| 213 | extends the Email class, you will use:: |
| 214 | |
| 215 | $this->load->library('email'); |
| 216 | |
| 217 | Once loaded you will use the class variable as you normally would for |
| 218 | the class you are extending. In the case of the email class all calls |
| 219 | will use:: |
| 220 | |
| 221 | $this->email->some_function(); |
| 222 | |
| 223 | Setting Your Own Prefix |
| 224 | ----------------------- |
| 225 | |
| 226 | To set your own sub-class prefix, open your |
| 227 | application/config/config.php file and look for this item:: |
| 228 | |
| 229 | $config['subclass_prefix'] = 'MY_'; |
| 230 | |
| 231 | Please note that all native CodeIgniter libraries are prefixed with CI\_ |
| 232 | so DO NOT use that as your prefix. |