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 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 57 | .. note:: We are using the name Someclass purely as an example. |
| 58 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 59 | Using Your Class |
| 60 | ================ |
| 61 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 62 | From within any of your :doc:`Controller <controllers>` methods you |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 73 | $this->someclass->some_method(); // Object instances will always be lower case |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
| 75 | Passing Parameters When Initializing Your Class |
| 76 | =============================================== |
| 77 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 78 | In the library loading method you can dynamically pass data as an |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 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 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 89 | <?php defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 90 | |
| 91 | class Someclass { |
| 92 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 93 | public function __construct($params) |
| 94 | { |
| 95 | // Do something with $params |
| 96 | } |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 99 | You can also pass parameters stored in a config file. Simply create a |
| 100 | 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] | 101 | your *application/config/* directory. Note that if you dynamically pass |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 102 | parameters as described above, the config file option will not be |
| 103 | available. |
| 104 | |
| 105 | Utilizing CodeIgniter Resources within Your Library |
| 106 | =================================================== |
| 107 | |
| 108 | To access CodeIgniter's native resources within your library use the |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 109 | ``get_instance()`` method. This method returns the CodeIgniter super |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | object. |
| 111 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 112 | Normally from within your controller methods you will call any of the |
| 113 | available CodeIgniter methods using the ``$this`` construct:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 115 | $this->load->helper('url'); |
| 116 | $this->load->library('session'); |
| 117 | $this->config->item('base_url'); |
| 118 | // etc. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 119 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 120 | ``$this``, however, only works directly within your controllers, your |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | models, or your views. If you would like to use CodeIgniter's classes |
| 122 | from within your own custom classes you can do so as follows: |
| 123 | |
| 124 | First, assign the CodeIgniter object to a variable:: |
| 125 | |
| 126 | $CI =& get_instance(); |
| 127 | |
| 128 | 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] | 129 | *instead* of ``$this``:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 131 | $CI =& get_instance(); |
| 132 | |
| 133 | $CI->load->helper('url'); |
| 134 | $CI->load->library('session'); |
| 135 | $CI->config->item('base_url'); |
| 136 | // etc. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 137 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 138 | .. note:: You'll notice that the above ``get_instance()`` function is being |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | passed by reference:: |
| 140 | |
| 141 | $CI =& get_instance(); |
| 142 | |
| 143 | This is very important. Assigning by reference allows you to use the |
| 144 | original CodeIgniter object rather than creating a copy of it. |
| 145 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 146 | However, since a library is a class, it would be better if you |
| 147 | take full advantage of the OOP principles. So, in order to |
| 148 | be able to use the CodeIgniter super-object in all of the class |
| 149 | methods, you're encouraged to assign it to a property instead:: |
| 150 | |
vlakoff | 024cfec | 2013-01-09 18:10:20 +0100 | [diff] [blame] | 151 | class Example_library { |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 152 | |
vlakoff | 024cfec | 2013-01-09 18:10:20 +0100 | [diff] [blame] | 153 | protected $CI; |
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 | // We'll use a constructor, as you can't directly call a function |
| 156 | // from a property definition. |
| 157 | public function __construct() |
| 158 | { |
| 159 | // Assign the CodeIgniter super-object |
| 160 | $this->CI =& get_instance(); |
| 161 | } |
| 162 | |
| 163 | public function foo() |
| 164 | { |
| 165 | $this->CI->load->helper('url'); |
| 166 | redirect(); |
| 167 | } |
| 168 | |
| 169 | public function bar() |
| 170 | { |
vlakoff | 891855d | 2014-09-20 08:48:27 +0200 | [diff] [blame] | 171 | echo $this->CI->config->item('base_url'); |
vlakoff | 024cfec | 2013-01-09 18:10:20 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 176 | Replacing Native Libraries with Your Versions |
| 177 | ============================================= |
| 178 | |
| 179 | Simply by naming your class files identically to a native library will |
| 180 | cause CodeIgniter to use it instead of the native one. To use this |
| 181 | feature you must name the file and the class declaration exactly the |
| 182 | same as the native library. For example, to replace the native Email |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 183 | library you'll create a file named *application/libraries/Email.php*, |
| 184 | and declare your class with:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 185 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 186 | class CI_Email { |
| 187 | |
| 188 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 189 | |
| 190 | Note that most native classes are prefixed with CI\_. |
| 191 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 192 | To load your library you'll see the standard loading method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 193 | |
| 194 | $this->load->library('email'); |
| 195 | |
| 196 | .. note:: At this time the Database classes can not be replaced with |
| 197 | your own versions. |
| 198 | |
| 199 | Extending Native Libraries |
| 200 | ========================== |
| 201 | |
| 202 | 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] | 203 | 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] | 204 | library with your version. In this case it's better to simply extend the |
| 205 | class. Extending a class is nearly identical to replacing a class with a |
| 206 | couple exceptions: |
| 207 | |
| 208 | - The class declaration must extend the parent class. |
| 209 | - Your new class name and filename must be prefixed with MY\_ (this |
| 210 | item is configurable. See below.). |
| 211 | |
| 212 | 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] | 213 | *application/libraries/MY_Email.php*, and declare your class with:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 214 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 215 | class MY_Email extends CI_Email { |
| 216 | |
| 217 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 218 | |
Andrey Andreev | 1d57197 | 2012-03-07 11:49:35 +0200 | [diff] [blame] | 219 | 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] | 220 | extend the parent constructor:: |
| 221 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 222 | class MY_Email extends CI_Email { |
| 223 | |
Andrey Andreev | 1d57197 | 2012-03-07 11:49:35 +0200 | [diff] [blame] | 224 | public function __construct($config = array()) |
| 225 | { |
| 226 | parent::__construct($config); |
Andrey Andreev | 4a56778 | 2017-11-20 09:58:03 +0200 | [diff] [blame^] | 227 | // Your own constructor code |
Andrey Andreev | 1d57197 | 2012-03-07 11:49:35 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 230 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 231 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 232 | .. note:: Not all of the libraries have the same (or any) parameters |
Andrey Andreev | 1d57197 | 2012-03-07 11:49:35 +0200 | [diff] [blame] | 233 | in their constructor. Take a look at the library that you're |
| 234 | extending first to see how it should be implemented. |
| 235 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 236 | Loading Your Sub-class |
| 237 | ---------------------- |
| 238 | |
| 239 | To load your sub-class you'll use the standard syntax normally used. DO |
| 240 | NOT include your prefix. For example, to load the example above, which |
| 241 | extends the Email class, you will use:: |
| 242 | |
| 243 | $this->load->library('email'); |
| 244 | |
| 245 | Once loaded you will use the class variable as you normally would for |
| 246 | the class you are extending. In the case of the email class all calls |
| 247 | will use:: |
| 248 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 249 | $this->email->some_method(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 250 | |
| 251 | Setting Your Own Prefix |
| 252 | ----------------------- |
| 253 | |
| 254 | To set your own sub-class prefix, open your |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 255 | *application/config/config.php* file and look for this item:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 256 | |
| 257 | $config['subclass_prefix'] = 'MY_'; |
| 258 | |
| 259 | Please note that all native CodeIgniter libraries are prefixed with CI\_ |
Andrey Andreev | 4a56778 | 2017-11-20 09:58:03 +0200 | [diff] [blame^] | 260 | so DO NOT use that as your prefix. |