blob: 673fbd4bbba60334cd8b3fbade0fc0e8b1148c42 [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
15identically named versions in your application/libraries folder.
16
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
31Your library classes should be placed within your application/libraries
32folder, as this is where CodeIgniter will look for them when they are
33initialized.
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
45Classes should have this basic prototype (Note: We are using the name
46Someclass purely as an example)::
47
Derek Jones9713cce2011-10-05 17:26:43 -050048 <?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 Jones8ede1a22011-10-05 13:34:52 -050058
59Using Your Class
60================
61
62From within any of your :doc:`Controller <controllers>` functions you
63can 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
73 $this->someclass->some_function();  // Object instances will always be lower case
74
75Passing Parameters When Initializing Your Class
76===============================================
77
78In the library loading function you can dynamically pass data as an
79array 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
84 $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
Derek Jones9713cce2011-10-05 17:26:43 -050089 <?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 Jones8ede1a22011-10-05 13:34:52 -0500100
101You 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
103your application/config/ folder. Note that if you dynamically pass
104parameters 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
111get_instance() function. This function returns the CodeIgniter super
112object.
113
114Normally from within your controller functions you will call any of the
115available CodeIgniter functions using the $this construct::
116
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
122$this, however, only works directly within your controllers, your
123models, 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
131*instead* of $this::
132
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
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
148Replacing Native Libraries with Your Versions
149=============================================
150
151Simply by naming your class files identically to a native library will
152cause CodeIgniter to use it instead of the native one. To use this
153feature you must name the file and the class declaration exactly the
154same as the native library. For example, to replace the native Email
155library you'll create a file named application/libraries/Email.php, and
156declare your class with::
157
Derek Jones9713cce2011-10-05 17:26:43 -0500158 class CI_Email {
159
160 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
162Note that most native classes are prefixed with CI\_.
163
164To 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
171Extending Native Libraries
172==========================
173
174If all you need to do is add some functionality to an existing library -
175perhaps add a function or two - then it's overkill to replace the entire
176library with your version. In this case it's better to simply extend the
177class. Extending a class is nearly identical to replacing a class with a
178couple 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
184For example, to extend the native Email class you'll create a file named
185application/libraries/MY_Email.php, and declare your class with::
186
Derek Jones9713cce2011-10-05 17:26:43 -0500187 class MY_Email extends CI_Email {
188
189 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500190
Andrey Andreev1d571972012-03-07 11:49:35 +0200191If you need to use a constructor in your class make sure you
Derek Jones8ede1a22011-10-05 13:34:52 -0500192extend the parent constructor::
193
Derek Jones9713cce2011-10-05 17:26:43 -0500194 class MY_Email extends CI_Email {
195
Andrey Andreev1d571972012-03-07 11:49:35 +0200196 public function __construct($config = array())
197 {
198 parent::__construct($config);
199 }
200
Derek Jones9713cce2011-10-05 17:26:43 -0500201 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
Andrey Andreev1d571972012-03-07 11:49:35 +0200203.. 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 Jones8ede1a22011-10-05 13:34:52 -0500208Loading Your Sub-class
209----------------------
210
211To load your sub-class you'll use the standard syntax normally used. DO
212NOT include your prefix. For example, to load the example above, which
213extends the Email class, you will use::
214
215 $this->load->library('email');
216
217Once loaded you will use the class variable as you normally would for
218the class you are extending. In the case of the email class all calls
219will use::
220
221 $this->email->some_function();
222
223Setting Your Own Prefix
224-----------------------
225
226To set your own sub-class prefix, open your
227application/config/config.php file and look for this item::
228
229 $config['subclass_prefix'] = 'MY_';
230
231Please note that all native CodeIgniter libraries are prefixed with CI\_
232so DO NOT use that as your prefix.