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 |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 15 | identically named versions in your *application/libraries* directory. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 31 | Your library classes should be placed within your *application/libraries* |
| 32 | directory, as this is where CodeIgniter will look for them when they are |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 45 | Classes should have this basic prototype:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | |
darwinel | d8bef8a | 2014-02-11 20:13:22 +0100 | [diff] [blame] | 47 | <?php |
| 48 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 49 | |
| 50 | class Someclass { |
| 51 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 52 | public function some_method() |
| 53 | { |
| 54 | } |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* End of file Someclass.php */ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 58 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 59 | .. note:: We are using the name Someclass purely as an example. |
| 60 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 61 | Using Your Class |
| 62 | ================ |
| 63 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 64 | From within any of your :doc:`Controller <controllers>` methods you |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 65 | can initialize your class using the standard:: |
| 66 | |
| 67 | $this->load->library('someclass'); |
| 68 | |
| 69 | Where *someclass* is the file name, without the ".php" file extension. |
| 70 | You can submit the file name capitalized or lower case. CodeIgniter |
| 71 | doesn't care. |
| 72 | |
| 73 | Once loaded you can access your class using the lower case version:: |
| 74 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 75 | $this->someclass->some_method(); // Object instances will always be lower case |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 76 | |
| 77 | Passing Parameters When Initializing Your Class |
| 78 | =============================================== |
| 79 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 80 | In the library loading method you can dynamically pass data as an |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 81 | array via the second parameter and it will be passed to your class |
| 82 | constructor:: |
| 83 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 84 | $params = array('type' => 'large', 'color' => 'red'); |
| 85 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 86 | $this->load->library('someclass', $params); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 87 | |
| 88 | If you use this feature you must set up your class constructor to expect |
| 89 | data:: |
| 90 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 91 | <?php defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 92 | |
| 93 | class Someclass { |
| 94 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 95 | public function __construct($params) |
| 96 | { |
| 97 | // Do something with $params |
| 98 | } |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 103 | your *application/config/* directory. Note that if you dynamically pass |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 111 | ``get_instance()`` method. This method returns the CodeIgniter super |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | object. |
| 113 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 114 | Normally from within your controller methods you will call any of the |
| 115 | available CodeIgniter methods using the ``$this`` construct:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 122 | ``$this``, however, only works directly within your controllers, your |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 131 | *instead* of ``$this``:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 140 | .. note:: You'll notice that the above ``get_instance()`` function is being |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 148 | However, since a library is a class, it would be better if you |
| 149 | take full advantage of the OOP principles. So, in order to |
| 150 | be able to use the CodeIgniter super-object in all of the class |
| 151 | methods, you're encouraged to assign it to a property instead:: |
| 152 | |
vlakoff | 024cfec | 2013-01-09 18:10:20 +0100 | [diff] [blame] | 153 | class Example_library { |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 154 | |
vlakoff | 024cfec | 2013-01-09 18:10:20 +0100 | [diff] [blame] | 155 | protected $CI; |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 156 | |
vlakoff | 024cfec | 2013-01-09 18:10:20 +0100 | [diff] [blame] | 157 | // We'll use a constructor, as you can't directly call a function |
| 158 | // from a property definition. |
| 159 | public function __construct() |
| 160 | { |
| 161 | // Assign the CodeIgniter super-object |
| 162 | $this->CI =& get_instance(); |
| 163 | } |
| 164 | |
| 165 | public function foo() |
| 166 | { |
| 167 | $this->CI->load->helper('url'); |
| 168 | redirect(); |
| 169 | } |
| 170 | |
| 171 | public function bar() |
| 172 | { |
| 173 | echo $this->CI->config_item('base_url'); |
| 174 | } |
| 175 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 178 | Replacing Native Libraries with Your Versions |
| 179 | ============================================= |
| 180 | |
| 181 | Simply by naming your class files identically to a native library will |
| 182 | cause CodeIgniter to use it instead of the native one. To use this |
| 183 | feature you must name the file and the class declaration exactly the |
| 184 | same as the native library. For example, to replace the native Email |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 185 | library you'll create a file named *application/libraries/Email.php*, |
| 186 | and declare your class with:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 187 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 188 | class CI_Email { |
| 189 | |
| 190 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 191 | |
| 192 | Note that most native classes are prefixed with CI\_. |
| 193 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 194 | To load your library you'll see the standard loading method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 195 | |
| 196 | $this->load->library('email'); |
| 197 | |
| 198 | .. note:: At this time the Database classes can not be replaced with |
| 199 | your own versions. |
| 200 | |
| 201 | Extending Native Libraries |
| 202 | ========================== |
| 203 | |
| 204 | If all you need to do is add some functionality to an existing library - |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 205 | perhaps add a method or two - then it's overkill to replace the entire |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 206 | library with your version. In this case it's better to simply extend the |
| 207 | class. Extending a class is nearly identical to replacing a class with a |
| 208 | couple exceptions: |
| 209 | |
| 210 | - The class declaration must extend the parent class. |
| 211 | - Your new class name and filename must be prefixed with MY\_ (this |
| 212 | item is configurable. See below.). |
| 213 | |
| 214 | For example, to extend the native Email class you'll create a file named |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 215 | *application/libraries/MY_Email.php*, and declare your class with:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 216 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 217 | class MY_Email extends CI_Email { |
| 218 | |
| 219 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 220 | |
Andrey Andreev | 1d57197 | 2012-03-07 11:49:35 +0200 | [diff] [blame] | 221 | 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] | 222 | extend the parent constructor:: |
| 223 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 224 | class MY_Email extends CI_Email { |
| 225 | |
Andrey Andreev | 1d57197 | 2012-03-07 11:49:35 +0200 | [diff] [blame] | 226 | public function __construct($config = array()) |
| 227 | { |
| 228 | parent::__construct($config); |
| 229 | } |
| 230 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 231 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 232 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 233 | .. note:: Not all of the libraries have the same (or any) parameters |
Andrey Andreev | 1d57197 | 2012-03-07 11:49:35 +0200 | [diff] [blame] | 234 | in their constructor. Take a look at the library that you're |
| 235 | extending first to see how it should be implemented. |
| 236 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 237 | Loading Your Sub-class |
| 238 | ---------------------- |
| 239 | |
| 240 | To load your sub-class you'll use the standard syntax normally used. DO |
| 241 | NOT include your prefix. For example, to load the example above, which |
| 242 | extends the Email class, you will use:: |
| 243 | |
| 244 | $this->load->library('email'); |
| 245 | |
| 246 | Once loaded you will use the class variable as you normally would for |
| 247 | the class you are extending. In the case of the email class all calls |
| 248 | will use:: |
| 249 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 250 | $this->email->some_method(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 251 | |
| 252 | Setting Your Own Prefix |
| 253 | ----------------------- |
| 254 | |
| 255 | To set your own sub-class prefix, open your |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 256 | *application/config/config.php* file and look for this item:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 257 | |
| 258 | $config['subclass_prefix'] = 'MY_'; |
| 259 | |
| 260 | Please note that all native CodeIgniter libraries are prefixed with CI\_ |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 261 | so DO NOT use that as your prefix. |