blob: 83742b619dfcb8fcd41051fa1865976e45a0e618 [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
Andrey Andreev20292312013-07-22 14:29:10 +030057.. note:: We are using the name Someclass purely as an example.
58
Derek Jones8ede1a22011-10-05 13:34:52 -050059Using Your Class
60================
61
Andrey Andreev16a704c2012-11-09 17:25:00 +020062From within any of your :doc:`Controller <controllers>` methods you
Derek Jones8ede1a22011-10-05 13:34:52 -050063can initialize your class using the standard::
64
65 $this->load->library('someclass');
66
67Where *someclass* is the file name, without the ".php" file extension.
68You can submit the file name capitalized or lower case. CodeIgniter
69doesn't care.
70
71Once loaded you can access your class using the lower case version::
72
Andrey Andreev16a704c2012-11-09 17:25:00 +020073 $this->someclass->some_method();  // Object instances will always be lower case
Derek Jones8ede1a22011-10-05 13:34:52 -050074
75Passing Parameters When Initializing Your Class
76===============================================
77
Andrey Andreev16a704c2012-11-09 17:25:00 +020078In the library loading method you can dynamically pass data as an
Derek Jones8ede1a22011-10-05 13:34:52 -050079array via the second parameter and it will be passed to your class
80constructor::
81
Derek Jones9713cce2011-10-05 17:26:43 -050082 $params = array('type' => 'large', 'color' => 'red');
83
Andrey Andreev20292312013-07-22 14:29:10 +030084 $this->load->library('someclass', $params);
Derek Jones8ede1a22011-10-05 13:34:52 -050085
86If you use this feature you must set up your class constructor to expect
87data::
88
Andrey Andreev16a704c2012-11-09 17:25:00 +020089 <?php defined('BASEPATH') OR exit('No direct script access allowed');
Derek Jones9713cce2011-10-05 17:26:43 -050090
91 class Someclass {
92
Andrey Andreev16a704c2012-11-09 17:25:00 +020093 public function __construct($params)
94 {
95 // Do something with $params
96 }
Derek Jones9713cce2011-10-05 17:26:43 -050097 }
98
Derek Jones8ede1a22011-10-05 13:34:52 -050099You can also pass parameters stored in a config file. Simply create a
100config file named identically to the class file name and store it in
Andrey Andreev16a704c2012-11-09 17:25:00 +0200101your *application/config/* directory. Note that if you dynamically pass
Derek Jones8ede1a22011-10-05 13:34:52 -0500102parameters as described above, the config file option will not be
103available.
104
105Utilizing CodeIgniter Resources within Your Library
106===================================================
107
108To access CodeIgniter's native resources within your library use the
Andrey Andreev16a704c2012-11-09 17:25:00 +0200109``get_instance()`` method. This method returns the CodeIgniter super
Derek Jones8ede1a22011-10-05 13:34:52 -0500110object.
111
Andrey Andreev16a704c2012-11-09 17:25:00 +0200112Normally from within your controller methods you will call any of the
113available CodeIgniter methods using the ``$this`` construct::
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Derek Jones9713cce2011-10-05 17:26:43 -0500115 $this->load->helper('url');
116 $this->load->library('session');
117 $this->config->item('base_url');
118 // etc.
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Andrey Andreev16a704c2012-11-09 17:25:00 +0200120``$this``, however, only works directly within your controllers, your
Derek Jones8ede1a22011-10-05 13:34:52 -0500121models, or your views. If you would like to use CodeIgniter's classes
122from within your own custom classes you can do so as follows:
123
124First, assign the CodeIgniter object to a variable::
125
126 $CI =& get_instance();
127
128Once you've assigned the object to a variable, you'll use that variable
Andrey Andreev16a704c2012-11-09 17:25:00 +0200129*instead* of ``$this``::
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
Derek Jones9713cce2011-10-05 17:26:43 -0500131 $CI =& get_instance();
132
133 $CI->load->helper('url');
134 $CI->load->library('session');
135 $CI->config->item('base_url');
136 // etc.
Derek Jones8ede1a22011-10-05 13:34:52 -0500137
Andrey Andreev16a704c2012-11-09 17:25:00 +0200138.. note:: You'll notice that the above ``get_instance()`` function is being
Derek Jones8ede1a22011-10-05 13:34:52 -0500139 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 Andreev16a704c2012-11-09 17:25:00 +0200146However, since a library is a class, it would be better if you
147take full advantage of the OOP principles. So, in order to
148be able to use the CodeIgniter super-object in all of the class
149methods, you're encouraged to assign it to a property instead::
150
vlakoff024cfec2013-01-09 18:10:20 +0100151 class Example_library {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200152
vlakoff024cfec2013-01-09 18:10:20 +0100153 protected $CI;
Andrey Andreev16a704c2012-11-09 17:25:00 +0200154
vlakoff024cfec2013-01-09 18:10:20 +0100155 // 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 {
vlakoff891855d2014-09-20 08:48:27 +0200171 echo $this->CI->config->item('base_url');
vlakoff024cfec2013-01-09 18:10:20 +0100172 }
173
Andrey Andreev16a704c2012-11-09 17:25:00 +0200174 }
175
Derek Jones8ede1a22011-10-05 13:34:52 -0500176Replacing Native Libraries with Your Versions
177=============================================
178
179Simply by naming your class files identically to a native library will
180cause CodeIgniter to use it instead of the native one. To use this
181feature you must name the file and the class declaration exactly the
182same as the native library. For example, to replace the native Email
Andrey Andreev16a704c2012-11-09 17:25:00 +0200183library you'll create a file named *application/libraries/Email.php*,
184and declare your class with::
Derek Jones8ede1a22011-10-05 13:34:52 -0500185
Derek Jones9713cce2011-10-05 17:26:43 -0500186 class CI_Email {
187
188 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500189
190Note that most native classes are prefixed with CI\_.
191
Andrey Andreev16a704c2012-11-09 17:25:00 +0200192To load your library you'll see the standard loading method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500193
194 $this->load->library('email');
195
196.. note:: At this time the Database classes can not be replaced with
197 your own versions.
198
199Extending Native Libraries
200==========================
201
202If all you need to do is add some functionality to an existing library -
Andrey Andreev16a704c2012-11-09 17:25:00 +0200203perhaps add a method or two - then it's overkill to replace the entire
Derek Jones8ede1a22011-10-05 13:34:52 -0500204library with your version. In this case it's better to simply extend the
205class. Extending a class is nearly identical to replacing a class with a
206couple 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
212For example, to extend the native Email class you'll create a file named
Andrey Andreev16a704c2012-11-09 17:25:00 +0200213*application/libraries/MY_Email.php*, and declare your class with::
Derek Jones8ede1a22011-10-05 13:34:52 -0500214
Derek Jones9713cce2011-10-05 17:26:43 -0500215 class MY_Email extends CI_Email {
216
217 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500218
Andrey Andreev1d571972012-03-07 11:49:35 +0200219If you need to use a constructor in your class make sure you
Derek Jones8ede1a22011-10-05 13:34:52 -0500220extend the parent constructor::
221
Derek Jones9713cce2011-10-05 17:26:43 -0500222 class MY_Email extends CI_Email {
223
Andrey Andreev1d571972012-03-07 11:49:35 +0200224 public function __construct($config = array())
225 {
226 parent::__construct($config);
Andrey Andreev4a567782017-11-20 09:58:03 +0200227 // Your own constructor code
Andrey Andreev1d571972012-03-07 11:49:35 +0200228 }
229
Derek Jones9713cce2011-10-05 17:26:43 -0500230 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500231
Andrey Andreev16a704c2012-11-09 17:25:00 +0200232.. note:: Not all of the libraries have the same (or any) parameters
Andrey Andreev1d571972012-03-07 11:49:35 +0200233 in their constructor. Take a look at the library that you're
234 extending first to see how it should be implemented.
235
Derek Jones8ede1a22011-10-05 13:34:52 -0500236Loading Your Sub-class
237----------------------
238
239To load your sub-class you'll use the standard syntax normally used. DO
240NOT include your prefix. For example, to load the example above, which
241extends the Email class, you will use::
242
243 $this->load->library('email');
244
245Once loaded you will use the class variable as you normally would for
246the class you are extending. In the case of the email class all calls
247will use::
248
Andrey Andreev16a704c2012-11-09 17:25:00 +0200249 $this->email->some_method();
Derek Jones8ede1a22011-10-05 13:34:52 -0500250
251Setting Your Own Prefix
252-----------------------
253
254To set your own sub-class prefix, open your
Andrey Andreev16a704c2012-11-09 17:25:00 +0200255*application/config/config.php* file and look for this item::
Derek Jones8ede1a22011-10-05 13:34:52 -0500256
257 $config['subclass_prefix'] = 'MY_';
258
259Please note that all native CodeIgniter libraries are prefixed with CI\_
Andrey Andreev4a567782017-11-20 09:58:03 +0200260so DO NOT use that as your prefix.