blob: a1e1b3e784d674cef209d4ead161df69639eb4c9 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##################
2Creating Libraries
3##################
4
5When we use the term "Libraries" we are normally referring to the
6classes that are located in the libraries directory and described in the
7Class Reference of this user guide. In this case, however, we will
8instead describe how you can create your own libraries within your
9application/libraries directory in order to maintain separation between
10your local resources and the global framework resources.
11
12As an added bonus, CodeIgniter permits your libraries to extend native
13classes if you simply need to add some functionality to an existing
14library. Or you can even replace native libraries just by placing
Andrey Andreev16a704c2012-11-09 17:25:00 +020015identically named versions in your *application/libraries* directory.
Derek Jones8ede1a22011-10-05 13:34:52 -050016
17In summary:
18
19- You can create entirely new libraries.
20- You can extend native libraries.
21- You can replace native libraries.
22
23The 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
28Storage
29=======
30
Andrey Andreev16a704c2012-11-09 17:25:00 +020031Your library classes should be placed within your *application/libraries*
32directory, as this is where CodeIgniter will look for them when they are
Derek Jones8ede1a22011-10-05 13:34:52 -050033initialized.
34
35Naming 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
42The Class File
43==============
44
Andrey Andreev20292312013-07-22 14:29:10 +030045Classes should have this basic prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050046
darwineld8bef8a2014-02-11 20:13:22 +010047 <?php
48 defined('BASEPATH') OR exit('No direct script access allowed');
Derek Jones9713cce2011-10-05 17:26:43 -050049
50 class Someclass {
51
Andrey Andreev16a704c2012-11-09 17:25:00 +020052 public function some_method()
53 {
54 }
Derek Jones9713cce2011-10-05 17:26:43 -050055 }
56
57 /* End of file Someclass.php */
Derek Jones8ede1a22011-10-05 13:34:52 -050058
Andrey Andreev20292312013-07-22 14:29:10 +030059.. note:: We are using the name Someclass purely as an example.
60
Derek Jones8ede1a22011-10-05 13:34:52 -050061Using Your Class
62================
63
Andrey Andreev16a704c2012-11-09 17:25:00 +020064From within any of your :doc:`Controller <controllers>` methods you
Derek Jones8ede1a22011-10-05 13:34:52 -050065can initialize your class using the standard::
66
67 $this->load->library('someclass');
68
69Where *someclass* is the file name, without the ".php" file extension.
70You can submit the file name capitalized or lower case. CodeIgniter
71doesn't care.
72
73Once loaded you can access your class using the lower case version::
74
Andrey Andreev16a704c2012-11-09 17:25:00 +020075 $this->someclass->some_method();  // Object instances will always be lower case
Derek Jones8ede1a22011-10-05 13:34:52 -050076
77Passing Parameters When Initializing Your Class
78===============================================
79
Andrey Andreev16a704c2012-11-09 17:25:00 +020080In the library loading method you can dynamically pass data as an
Derek Jones8ede1a22011-10-05 13:34:52 -050081array via the second parameter and it will be passed to your class
82constructor::
83
Derek Jones9713cce2011-10-05 17:26:43 -050084 $params = array('type' => 'large', 'color' => 'red');
85
Andrey Andreev20292312013-07-22 14:29:10 +030086 $this->load->library('someclass', $params);
Derek Jones8ede1a22011-10-05 13:34:52 -050087
88If you use this feature you must set up your class constructor to expect
89data::
90
Andrey Andreev16a704c2012-11-09 17:25:00 +020091 <?php defined('BASEPATH') OR exit('No direct script access allowed');
Derek Jones9713cce2011-10-05 17:26:43 -050092
93 class Someclass {
94
Andrey Andreev16a704c2012-11-09 17:25:00 +020095 public function __construct($params)
96 {
97 // Do something with $params
98 }
Derek Jones9713cce2011-10-05 17:26:43 -050099 }
100
Derek Jones8ede1a22011-10-05 13:34:52 -0500101You can also pass parameters stored in a config file. Simply create a
102config file named identically to the class file name and store it in
Andrey Andreev16a704c2012-11-09 17:25:00 +0200103your *application/config/* directory. Note that if you dynamically pass
Derek Jones8ede1a22011-10-05 13:34:52 -0500104parameters as described above, the config file option will not be
105available.
106
107Utilizing CodeIgniter Resources within Your Library
108===================================================
109
110To access CodeIgniter's native resources within your library use the
Andrey Andreev16a704c2012-11-09 17:25:00 +0200111``get_instance()`` method. This method returns the CodeIgniter super
Derek Jones8ede1a22011-10-05 13:34:52 -0500112object.
113
Andrey Andreev16a704c2012-11-09 17:25:00 +0200114Normally from within your controller methods you will call any of the
115available CodeIgniter methods using the ``$this`` construct::
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Derek Jones9713cce2011-10-05 17:26:43 -0500117 $this->load->helper('url');
118 $this->load->library('session');
119 $this->config->item('base_url');
120 // etc.
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
Andrey Andreev16a704c2012-11-09 17:25:00 +0200122``$this``, however, only works directly within your controllers, your
Derek Jones8ede1a22011-10-05 13:34:52 -0500123models, or your views. If you would like to use CodeIgniter's classes
124from within your own custom classes you can do so as follows:
125
126First, assign the CodeIgniter object to a variable::
127
128 $CI =& get_instance();
129
130Once you've assigned the object to a variable, you'll use that variable
Andrey Andreev16a704c2012-11-09 17:25:00 +0200131*instead* of ``$this``::
Derek Jones8ede1a22011-10-05 13:34:52 -0500132
Derek Jones9713cce2011-10-05 17:26:43 -0500133 $CI =& get_instance();
134
135 $CI->load->helper('url');
136 $CI->load->library('session');
137 $CI->config->item('base_url');
138 // etc.
Derek Jones8ede1a22011-10-05 13:34:52 -0500139
Andrey Andreev16a704c2012-11-09 17:25:00 +0200140.. note:: You'll notice that the above ``get_instance()`` function is being
Derek Jones8ede1a22011-10-05 13:34:52 -0500141 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 Andreev16a704c2012-11-09 17:25:00 +0200148However, since a library is a class, it would be better if you
149take full advantage of the OOP principles. So, in order to
150be able to use the CodeIgniter super-object in all of the class
151methods, you're encouraged to assign it to a property instead::
152
vlakoff024cfec2013-01-09 18:10:20 +0100153 class Example_library {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200154
vlakoff024cfec2013-01-09 18:10:20 +0100155 protected $CI;
Andrey Andreev16a704c2012-11-09 17:25:00 +0200156
vlakoff024cfec2013-01-09 18:10:20 +0100157 // 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 Andreev16a704c2012-11-09 17:25:00 +0200176 }
177
Derek Jones8ede1a22011-10-05 13:34:52 -0500178Replacing Native Libraries with Your Versions
179=============================================
180
181Simply by naming your class files identically to a native library will
182cause CodeIgniter to use it instead of the native one. To use this
183feature you must name the file and the class declaration exactly the
184same as the native library. For example, to replace the native Email
Andrey Andreev16a704c2012-11-09 17:25:00 +0200185library you'll create a file named *application/libraries/Email.php*,
186and declare your class with::
Derek Jones8ede1a22011-10-05 13:34:52 -0500187
Derek Jones9713cce2011-10-05 17:26:43 -0500188 class CI_Email {
189
190 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500191
192Note that most native classes are prefixed with CI\_.
193
Andrey Andreev16a704c2012-11-09 17:25:00 +0200194To load your library you'll see the standard loading method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500195
196 $this->load->library('email');
197
198.. note:: At this time the Database classes can not be replaced with
199 your own versions.
200
201Extending Native Libraries
202==========================
203
204If all you need to do is add some functionality to an existing library -
Andrey Andreev16a704c2012-11-09 17:25:00 +0200205perhaps add a method or two - then it's overkill to replace the entire
Derek Jones8ede1a22011-10-05 13:34:52 -0500206library with your version. In this case it's better to simply extend the
207class. Extending a class is nearly identical to replacing a class with a
208couple 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
214For example, to extend the native Email class you'll create a file named
Andrey Andreev16a704c2012-11-09 17:25:00 +0200215*application/libraries/MY_Email.php*, and declare your class with::
Derek Jones8ede1a22011-10-05 13:34:52 -0500216
Derek Jones9713cce2011-10-05 17:26:43 -0500217 class MY_Email extends CI_Email {
218
219 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500220
Andrey Andreev1d571972012-03-07 11:49:35 +0200221If you need to use a constructor in your class make sure you
Derek Jones8ede1a22011-10-05 13:34:52 -0500222extend the parent constructor::
223
Derek Jones9713cce2011-10-05 17:26:43 -0500224 class MY_Email extends CI_Email {
225
Andrey Andreev1d571972012-03-07 11:49:35 +0200226 public function __construct($config = array())
227 {
228 parent::__construct($config);
229 }
230
Derek Jones9713cce2011-10-05 17:26:43 -0500231 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500232
Andrey Andreev16a704c2012-11-09 17:25:00 +0200233.. note:: Not all of the libraries have the same (or any) parameters
Andrey Andreev1d571972012-03-07 11:49:35 +0200234 in their constructor. Take a look at the library that you're
235 extending first to see how it should be implemented.
236
Derek Jones8ede1a22011-10-05 13:34:52 -0500237Loading Your Sub-class
238----------------------
239
240To load your sub-class you'll use the standard syntax normally used. DO
241NOT include your prefix. For example, to load the example above, which
242extends the Email class, you will use::
243
244 $this->load->library('email');
245
246Once loaded you will use the class variable as you normally would for
247the class you are extending. In the case of the email class all calls
248will use::
249
Andrey Andreev16a704c2012-11-09 17:25:00 +0200250 $this->email->some_method();
Derek Jones8ede1a22011-10-05 13:34:52 -0500251
252Setting Your Own Prefix
253-----------------------
254
255To set your own sub-class prefix, open your
Andrey Andreev16a704c2012-11-09 17:25:00 +0200256*application/config/config.php* file and look for this item::
Derek Jones8ede1a22011-10-05 13:34:52 -0500257
258 $config['subclass_prefix'] = 'MY_';
259
260Please note that all native CodeIgniter libraries are prefixed with CI\_
Andrey Andreev16a704c2012-11-09 17:25:00 +0200261so DO NOT use that as your prefix.